Random number diff from cmd line vs double click
Posted: 11 Apr 2014 04:30
I was helping a user on SO yesterday when we stumbled upon a strange occurrence with random numbers. Using the following code we want to create a random number between 11 and 21 and again between 0 and 59. When I was testing it, I ran it from a cmd prompt about 20 times and all was fine. But if that script is ran by double clicking on it, the first random number is always the same and it appears to be related to the system clock in some way because during our testing, we found that the random number went up by 1 after the hour on the system clock changed.
Does anyone have any idea what is going on here?
Code: Select all
@echo off
setlocal enabledelayedexpansion
call :rand 11 21 ret
call :rand 0 59 ret2
if %ret2% LSS 10 set ret2=0%ret2%
%ret%:%ret2%:00
pause
exit /b
:rand
setlocal
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
endlocal & set %~3=%RAND_NUM%
exit /b
Does anyone have any idea what is going on here?