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)
Aucun commentaire:
Enregistrer un commentaire