(Because someone asked, I have Windows 10, if anyone was wondering.
I have recently been working on a desktop PAI (Partial Artificial Intelligence) that will be able to perform some basic tasks for me while I am not at my computer.
However, I want it to choose and say 1 of 9 different possible messages. For example, I have 9 different ways to say “How can I help you?”, and I want it to choose and echo one of those 9 ways.
To do this, I want to use the variable %RANDOM%. The problem is that %RANDOM% generates a random number in between 0 and 32767, and I only want it to generate a random number in between 1 and 9.
I have seen multiple people across the internet say to use the piece of code:
SET /a rand=(%RANDOM%*9/32768)+1
However, this does not work. Or maybe it does for some people, but certainly not for me. Whenever I implement this into a batch file, it either just closes the window because it doesn’t know how to run the code, or it just chooses the same number over and over again, no matter what.
I have also already tried it with and without Delayed Expansion, and if I try to run the code in the regular command prompt (CMD.EXE), it just says “Missing operator.”
It’s hard for me because I don’t really know much about arithmetic in batch. Any help on fixing this is greatly appreciated!
2
Answers
What happens if you use
set /a rnd=(%RANDOM%%%9)+1
instead?Example:
When you state that you constantly get the same value when you run your script, I suspect you are launching your script by double clicking the script within Windows Explorer, or else on your desktop.
The source of that problem is the random seed value that each cmd.exe session establishes upon startup. The seed changes by 3 or 4 only once every second, (10 every 3 seconds).
Each time you launch your script by double clicking, you start a new cmd.exe session. All sessions that launch within the same second will be given the exact same random seed, and thus the same sequence.
Your problem is compounded by the formula you use to generate a “random” number from 1 to 9:
SET /a "rand=(%RANDOM%*9/32768)+1"
With that formula the initial result will change only once every ~18 minutes.A simpler formula of
set /a "rand=%random% %% 9 + 1"
will change the initial value once every second.Here is a demonstration of what I mean.
test.bat
Here is what happens when I use a loop to run the script 25 times in different cmd.exe sessions:
t is the time, r is the raw random seed value, n1 is the value computed by your original formula, and n2 is the value with the simpler formula. You can see that your original formula result never changes, even though the seed value changed 3 times.
If I repeat the experiment, but this time run each command within the same cmd.exe session, then you see that the random number sequence is reasonable.
There really isn’t a good pure batch solution for randomly generating numbers across different cmd.exe sessions. But you can use hybrid scripting to get a decent solution. Below is a demonstration JScript/batch solution that works well. However it is slow because the :random function must initialize a new CSCRIPT session each time it is called.
test2.bat
Here are results of rapidly running the above in 25 different cmd.exe sessions – exactly the result you want.