1

From this article, I have determined how to generate infinite looping unix time for a batch file pseudo-randomization number generator. Here's the script;

@echo off
cls
color 0a
title TimeRandom
set div=1
setlocal

:setbound
cls
set /p lowbound=InputLowbound:
set /p maxbound=InputMaxbound:
goto :loopuntil

:loopuntil
cls
call :GetUnixTime utime
set /a varnum=%utime%+%div%/%div%
if %varnum% LEQ %maxbound% goto :chk

:continue
set /a div=%utime%*%div%
goto :loopuntil

:chk
cls
if %varnum% GEQ %lowbound% goto :result
goto :continue

:result
cls
echo Random Result
echo.
echo %varnum%
echo.
pause
goto :setbound


:GetUnixTime
setlocal enableextensions
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
    set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
endlocal & set "%1=%ut%" & goto :EOF

Basically, at the line 17 and 21, I've tried numerous mathematical functions and still can't figure out the right function which is appropriate to use with this batch file considering the output time frame of no more than 10 seconds and generation of true pseudo-random numbers.

Update 1 : After some time,I was able to came up with a PRNG batch script which utilize Euclidean Division Algorithm. Here's the script now;

@echo off
cls
color 0a
title TimeRandom
set constant=29813
set div=1
setlocal

:choice
cls
echo.
echo Please Choose between,
echo.
echo [1].Precision mode : Less error and more precise. Requires some time to process.
echo [2].Coarse mode : Prone to errors with small number generation. Instant calculation.
echo.
set /p cho=Choice(1/2):

if %cho%==1 set /a add=1 && goto :setbound
if %cho%==2 set /a add=10 && goto :setbound
goto :choice

:setbound
cls
set /p lowbound=InputLowbound:
set /p maxbound=InputMaxbound:
goto :loopuntil

:loopuntil
cls
call :GetUnixTime utime
set /a er1=%utime%/%constant%
set /a valuer1=%utime%-(%er1%*%constant%)
if %valuer1%==0 goto :mainrandomc
set /a er2=%constant%/%valuer1%
set /a valuer2=%constant%-(%er2%*%valuer1%)
if %valuer2%==0 goto :mainrandom1
set /a er3=%valuer1%/%valuer2%
set /a valuer3=%valuer1%-(%er3%*%valuer2%)
if %valuer3%==0 goto :mainrandom2
set /a er4=%valuer2%/%valuer3%
set /a valuer4=%valuer2%-(%er4%*%valuer3%)
if %valuer4%==0 goto :mainrandom3
set /a er5=%valuer3%/%valuer4%
set /a valuer5=%valuer3%-(%er5%*%valuer4%)
if %valuer5%==0 goto :mainrandom4
set /a er6=%valuer4%/%valuer5%
set /a valuer6=%valuer4%-(%er6%*%valuer5%)
if %valuer6%==0 goto :mainrandom5
set /a er7=%valuer5%/%valuer6%
set /a valuer7=%valuer5%-(%er7%*%valuer6%)
if %valuer7%==0 goto :mainrandom6
set /a er8=%valuer6%/%valuer7%
set /a valuer8=%valuer6%-(%er8%*%valuer7%)
if %valuer8%==0 goto :mainrandom7
set /a er9=%valuer7%/%valuer8%
set /a valuer9=%valuer7%-(%er9%*%valuer8%)
if %valuer9%==0 goto :mainrandom8
set /a er10=%valuer8%/%valuer9%
set /a valuer10=%valuer8%-(%er10%*%valuer9%)
if %valuer10%==0 goto :mainrandom9
goto :EOF


:mainrandomc
cls
set /a varnum=%constant%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+%add%
goto :mainrandomc

:mainrandom1
cls
set /a varnum=%valuer1%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+%add%
goto :mainrandom1

:mainrandom2
cls
set /a varnum=%valuer2%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+%add%
goto :mainrandom2

:mainrandom3
cls
set /a varnum=%valuer3%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+%add%
goto :mainrandom3

:mainrandom4
cls
set /a varnum=%valuer4%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+%add%
goto :mainrandom4

:mainrandom5
cls
set /a varnum=%valuer5%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+%add%
goto :mainrandom5

:mainrandom6
cls
set /a varnum=%valuer6%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+1
goto :mainrandom6

:mainrandom7
cls
set /a varnum=%valuer7%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+1
goto :mainrandom7

:mainrandom8
cls
set /a varnum=%valuer8%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+1
goto :mainrandom7

:mainrandom9
cls
set /a varnum=%valuer9%/%div%
if %varnum% LEQ %maxbound% set /a vh=1
if %varnum% GEQ %lowbound% set /a vh=2
if %vh%==2 goto :result
set /a div=%div%+1
goto :mainrandom7


:result
cls
echo Random Result
echo.
echo %varnum%
echo.
pause
goto :setbound


:GetUnixTime
setlocal enableextensions
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
    set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
endlocal & set "%1=%ut%" & goto :EOF

Help me fix some randomization errors and unexpected termination of my program if possible.

kornkaobat
  • 93
  • 9
  • 2
    Firstly it should not read `set /a varnum=%utime%+%div%/%div%` but `set /a varnum=(utime+div)/div` and `set /a div=utime*div` not `set /a div=%utime%*%div%`. Secondly `Set /A` can only work with integers and those are limited. – Compo Jan 17 '19 at 17:35
  • You could even use `set /a div *=utime` instead of my previously suggested `set /a div=utime*div`. Take a look at the output from `Set /?` for the command usage information. – Compo Jan 18 '19 at 00:10
  • I just figured that euclid division could be the answer to this problem as it works with large numbers quite effectively. I’ll fix the code as you suggested too @Compo. – kornkaobat Jan 18 '19 at 01:42
  • @Compo Could you check my new answer to this problem and see if there's any problem? – kornkaobat Jan 20 '19 at 13:00

1 Answers1

0

The tracert command randomly takes about 50-90 seconds to finish, depending on ISP,IIG and server jumping. I've made a script which measure the time which took to run this command and use that number to calculate a pseudo-random number.

@echo off
cls
color 0a
title TimeRandom
set reduct=1
set increase=1
set div=2
setlocal

:notice
cls
echo TimeRandom uses tracert on some specific domain as a source of randomness.
echo.
echo Try to set the most furthest server from your location or the server
echo with the highest ping.
echo.
echo For generation of numbers below or equal to 60, You should make sure
echo the tracert command runs for at least 70 seconds or more for true randomness.
echo.
echo This program automatically adjusts itself to high maxbound numbers and or low valuerange
echo.
echo Boundaries ranging from 1-60 runtime : 75-90 seconds (est.)
echo Boundaries ranging from 61-1073741824 runtime : 90 seconds (est.)
echo Bizarre low valuerange boundaries : 90 seconds (est.)
echo.
echo The code author does not guarantee that this code generate true random numbers. Use with caution.
echo.
echo The code author should not be responsible for the misuse of this code against all laws and treaties.
echo.
set /p ackn=Continue(Y/N):

if %ackn%==Y goto :setbound
if %ackn%==y goto :setbound 
goto :EOF  


:setbound
cls
set /p lowbound=InputLowbound:
set /p maxbound=InputMaxbound:
if %maxbound% LEQ %lowbound% goto :EOF
goto :init

:init
cls
call :GetUnixTime utimebefore
tracert www.roblox.com
call :GetUnixTime utimeafter
set /a deltaubm=%utimeafter%-%utimebefore%
if %maxbound% LEQ 60 set /a deltau=%deltaubm% && goto :chkfn
goto :initincr

:initincr
cls
set /a med=(%lowbound%+%maxbound%)/2
set /a deltaubm=%deltaubm%*%deltaubm%
if %deltaubm% GTR %maxbound% goto :reductexpo
goto :initincr

:reductexpo
cls
set /a deltaubm=%deltaubm%/%div%
if %deltaubm% LEQ %maxbound% set deltau=%deltaubm% && goto :chkfn
goto :reductexpo

:chkfn
if %deltau% GTR %maxbound% goto :reductloop
if %deltau% EQU %maxbound% set varnum=%deltau% && goto :result
if %deltau% GTR %lowbound% set varnum=%deltau% && goto :result
if %deltau% EQU %lowbound% set varnum=%deltau% && goto :result
if %deltau% LSS %lowbound% goto :incrloop

:reductloop
cls
set /a deltau=%deltau%-%reduct%
set /a reduct=%reduct%+1
if %deltau% GTR %maxbound% goto :reductloop
if %deltau% EQU %maxbound% set varnum=%deltau% && goto :result
if %deltau% GTR %lowbound% set varnum=%deltau% && goto :result
if %deltau% EQU %lowbound% set varnum=%deltau% && goto :result
if %deltau% LSS %lowbound% goto :incrloop

:incrloop
cls
set /a deltau=%deltau%+%increase%
set /a increase=%increase%+1
if %deltau% GTR %maxbound% goto :reductloop
if %deltau% EQU %maxbound% set varnum=%deltau% && goto :result
if %deltau% GTR %lowbound% set varnum=%deltau% && goto :result
if %deltau% EQU %lowbound% set varnum=%deltau% && goto :result
if %deltau% LSS %lowbound% goto :incrloop

:result
cls
echo Random Result
echo.
echo %varnum%
echo.
pause
goto :setbound


:GetUnixTime
setlocal enableextensions
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
    set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
endlocal & set "%1=%ut%" & goto :EOF

Even though the time it took to run the script doesn't meet my requirements, it is still acceptable (in my view) as it generates random numbers not related with each other.

kornkaobat
  • 93
  • 9