90Views10Replies
No Surprise, I need more batch help
Is there a way to have %random% kept to a certian limit? like 1-10? if so the code would be nice, if no code, then jsut tell me how and i will do it. Thanks
Is there a way to have %random% kept to a certian limit? like 1-10? if so the code would be nice, if no code, then jsut tell me how and i will do it. Thanks
Comments
12 years ago
You will need to look up what range the random number generator will generate. Normalize that number with an expression or expressions that will scale it from 0 to 10. If you need whole numbers, there might be a rounding function.
Reply 12 years ago
The %random% variable generates a random integer from 0 to 32767. As for using the standard way of math in CMD, Batch, etc. it automatically rounds it down to the nearest integer.
Reply 12 years ago
And there is the whole business where it is not truly random so you have to seed it with something pseudo-random, like the time... :)
Reply 8 years ago
Well actually, no number can be randomly generated, but all of them are extremely hard to find out the pattern.
Why do you think nobody has found out the secret to KENO (I don't know if you know, but KENO is a gambling thing where I live).
It's not as easy as you think.
8 years ago
Actually, I'm changing my answer.
set /a number=%random% %% max + min
Where "max" is the maximum number you want, and "min" is the minimum number.
Example:
set /a dice=%random% %% 6 + 1
The variable "dice" would be set to a random number from 1 to 6 exclusive.
8 years ago
Thats easy, create a randomnum variable and set it as random and use the IF and GTR commands
:random
set /p randomnum=%random%
If %randomnum% GTR 10 goto random
12 years ago
Set /a random=%random% / 2978
Note: There's like a .0002% chance it will come up with 11. To prevent this, change it to 2979. It will just make the 10 appear .0002% less often.
12 years ago
or
Reply 12 years ago
the second option may freeze for quite a while . .
the most common way is mod division to the values you need
for example you need ramdom between 1 and 10 including
X = %random% mod 10 + 1
you may need help of an external app to do the calculation
Reply 12 years ago
lol thanks again, always there when i need batch help :D