Trying to generate a random number and use it for variable value in Windows batch script file. Maybe I am missing something very basic, but something is not working here for me.
Created a batch file named random_test.bat
with the following content.
SET rnumber=%random%
echo %random%
pause
Running the file consecutively three times produces the following set of outputs:
One
C:\Users\user\Desktop>SET rnumber=28955
C:\Users\user\Desktop>echo 20160
20160
C:\Users\user\Desktop>pause
Press any key to continue . . .
Two
C:\Users\user\Desktop>SET rnumber=29072
C:\Users\user\Desktop>echo 13887
13887
C:\Users\user\Desktop>pause
Press any key to continue . . .
Three
C:\Users\user\Desktop>SET rnumber=29183
C:\Users\user\Desktop>echo 18885
18885
C:\Users\user\Desktop>pause
Press any key to continue . . .
As you can see the command echo %random%
keeps producing relatively random numbers between 0 and 32,767 as expected.
At the same time using %random%
to set a value for variable rnumber
does not. It produces a not-so random number (possibly too between 0 and 32,767) but it is not random. If I were to guess right now it seems to be slowly growing in the 0 to 32,767 direction.
To clarify the script keeps producing a random number with line 2 on each execution (20160, 13887, 18885...), but line 1 seems to produce a number that keeps increasing with each execution of the batch file (28955, 29072, 29183, and so on in my multiple tests).
I already tried it on two different computers, Windows 7 x64 and Windows 2012 R2 respectively, running this 3 line script multiple times in row.
Next thing to try will be on a computer from a completely different network, I'm wondering if this has anything to do with domain policies, network, software..
What is going on here?
UPDATE:
When running the commands in sequence from the same CMD window it works as expected, but it does not when executing the same batch file multiple times.
- The line in the script
echo %random%
works as expected. - The line
SET rnumber=%random%
does not.
(When executing the same script multiple times)
Random numbers from cmd.exe is not for the faint of heart. Neither %RANDOM% nor !RANDOM! will get you there.
This page, https://ss64.com/nt/syntax-random.html, makes for interesting reading.
If you are on a supported Windows platform, PowerShell will work.
To provide a summary of everything that was learned from the comment discussion, external resources and testing regarding my original question.
Random number generator in batch script uses the following algorithm to seed the initial random number value when a new CMD window is opened. (from here - https://devblogs.microsoft.com/oldnewthing/?p=13673)
As a result when two(or more) CMD windows are started within the same second the initial returns of the %random% are the same. Subsequent returns of %random% within two CMD windows with the same seed and run time (to a second) are also the same.
Moreover if CMD windows are consequently started with certain delays from each other the first return of the %random% pseudo-variable in each new CMD window will be far from random and will be slowly increasing (0 to 32767 range). In my testing this initial seed number grows by either 3 or 4 every one second for each new window.
To learn more about this and to find workarounds look here:
https://ss64.com/nt/syntax-random.html
Why does rand() yield the same sequence of numbers on every run?
Also look at PowerShell workaround provided by @lit.