# COMMUNITY : FORUMS : TECH

## 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

sort by: active | newest | oldest
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.
8 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.
8 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... :)
4 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.
Prof. Pickle4 years ago

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.
Prof. Pickle5 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
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.
NachoMahma8 years ago
. In pseudocode:
`   GET %random%   IF %random% > 10 THEN %random% = 10   IF %random% < 1 THEN %random% = 1`
or
`   getrdm: GET %random%                  IF %random% > 10 THEN GOTO getrdm                  IF %random% < 1 THEN GOTO getrdm`
8 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
spiker (author)  NachoMahma8 years ago
lol thanks again, always there when i need batch help :D