90Views10Replies

Author Options:

No Surprise, I need more batch help Answered

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

The forums are retiring in 2021 and are now closed for new topics and comments.
0
caitlinsdad
caitlinsdad

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.

0
Padlock
Padlock

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.

0
caitlinsdad
caitlinsdad

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... :)

0
Prof. Pickle
Prof. Pickle

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.

0
Prof. Pickle
Prof. Pickle

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.

0
Prof. Pickle
Prof. Pickle

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

0
Padlock
Padlock

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.

0
NachoMahma
NachoMahma

12 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
0
11010010110
11010010110

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

0
spiker
spiker

Reply 12 years ago

lol thanks again, always there when i need batch help :D