从this article开始,我确定了如何为批处理文件伪随机数生成器生成无限循环unix时间。这是脚本;
@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
基本上,在第17和21行,我尝试了许多数学函数,但考虑到输出时间框架不超过10秒,并且仍然找不到适合该批处理文件的正确函数,并且真正的伪随机数的生成。
更新1:一段时间后,我想出了一个利用欧几里得除法算法的PRNG批处理脚本。现在是脚本;
@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
如果可能的话,请帮助我修复一些随机错误和程序意外终止。
答案 0 :(得分:0)
根据 ISP,IIG和服务器跳转,tracert
命令随机花费约50-90秒的时间来完成。我制作了一个脚本,用于测量运行此命令所花费的时间,并使用该数字来计算伪随机数。
@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
即使运行脚本所花费的时间不符合我的要求,但在我看来,它仍然可以接受,因为它会生成彼此不相关的随机数。