0

I'm trying to launch a random file with this software via a script - the only problem is that this script always selects the same random number on every launch. It's always 41 for me... Any suggestions?

@echo on
setlocal EnableDelayedExpansion
cd C:\Users\User\Documents\Downloads\Nintendo
set n=0
for %%f in (*.*) do (
   set /A n+=1
   set "file[!n!]=%%f"
)
set /A "rand=%random% * 100 / 32768+1"
"C:\Users\User\Downloads\fceux-2.2.2-win32\fceux.exe" "!file[%rand%]!"
b3b0
  • 3
  • 4

1 Answers1

0

That works for me:

@echo off
setlocal enabledelayedexpansion
c:
cd \windows

for %%f in (*.*) do (
   set /A n+=1
   set "file[!n!]=%%f"
)

rem change random seed value
for /l %%i in (0,1,%time:~-2%) do set /A tmp=!random!

set /A rand=!random! %% n-1
echo "!file[%rand%]!"
kay27
  • 897
  • 7
  • 16
  • Actually, the issue is a little more confusing than first thought. Anytime I run this script, I get the same value for "!file[%rand%]!", but only for a certain amount of time. Let's say I run the script, I get 63 - it opens and executes this flawlessly. I close the window and run the script again fifteen seconds later, I get 63 again. After maybe a minute or so has passed, I run the script, and I get 64 or 65... each execution of the script does not give me a random number, but is actually pretty predictable depending on how much time has passed since the last execution... What am I missing? – b3b0 Apr 19 '16 at 17:37
  • 1) it's a low precision of DOS arithmetics, so don't multiply/then/divide, just divide by 327 or better do as described here: http://stackoverflow.com/a/7963196/5920627 or here: http://stackoverflow.com/a/17639500/5920627 and you will get better result; 2) when you execute your batch file, there is some seed initialisation based on system time, so it will then produce the same number until the time will changed (number of seconds). – kay27 Apr 19 '16 at 20:15
  • i've tested %time% variable and found that last 2 characters are fraction part of a seconds, so it could be used to change random generator seed value more frequently (100 times per second instead of 1), so I updated my answer... I've no Downloads\Nintendo dir in my fs so I used c:\windows to test and now it shows a new filename every time I run it – kay27 Apr 19 '16 at 20:27
  • Very informative! Thanks so much for the help kay! You hit the nail on the head with this answer. – b3b0 Apr 20 '16 at 22:59