Generating Random Number In A Range

B

Barney

I need to generate a random number in a range between say
MIN and MAX.

I came across this thread on Google:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-
8&threadm=ZHn88.2089%24e5.188600%40news-
rep.ab.videon.ca&rnum=2&prev=/groups%3Fhl%3Den%26lr%3D%
26ie%3DUTF-8%26oe%3DUTF-8%26q%3D%2525Random%2525%26btnG%
3DSearch%26meta%3Dgroup%253Dalt.msdos.batch.nt

There are two approaches that appear do what I want but I
would like to know how/why they work. Specifically:

Why does
SET /A RND=%Random:~1,1%+1
generate a random number between 0 and 4?

Why does
SET /A RND="%RANDOM:%%%5"
generate a random number between 0 and 5?

or interactively

SET /A RND=%RANDOM%^%5?

Could someone explain the syntax for me?
 
M

Matthias Tacke

Barney said:
I need to generate a random number in a range between say
MIN and MAX.

I came across this thread on Google:

There are two approaches that appear do what I want but I
would like to know how/why they work. Specifically:

Why does
SET /A RND=%Random:~1,1%+1
generate a random number between 0 and 4?

In fact it generates a number between 0 and 2^15 and takes the second
place (from begin) of that.
So it should return a number between 1 and 10.
Why does
SET /A RND="%RANDOM:%%%5"
generate a random number between 0 and 5?
It's simply math. The % returns the rest of an integer division through
five. So the rest has to be between 0 and 4.
or interactively

SET /A RND=%RANDOM%^%5?

Could someone explain the syntax for me?

Look at the output of the following batch.
The results are right justified. To see the effect the random is only
used once in the sub.

@echo off
for /L %%A in (1,1,20) do call :rand
goto :eof
:rand
set RND0=%random%
SET /A RND1=%RND0:~1,1%+1
SET /A RND2=%RND0% %%5
set "RAND0= %RND0%
set "RAND1= %RND1%
set "RAND2= %RND2%
echo RANDO=%RAND0:~-5% RAND1=%RAND1:~-5% RAND2=%RAND2:~-5%

HTH
 
B

Barney

-----Original Message-----


In fact it generates a number between 0 and 2^15 and takes the second
place (from begin) of that.
So it should return a number between 1 and 10.

Of course, I don't know why I didn't see this. Thanks
Matthias.


It's simply math. The % returns the rest of an integer
division through five. So the rest has to be between 0
and 4.Which %? Why the colon :))? Why is the expression
enclosed in quotes ("")? Could you expand on this for me
or point me in the direction of some documentation.
Typing SET /? doesn't help me much.
 
M

Matthias Tacke

Barney said:
division through five. So the rest has to be between 0
and 4.
Which %? Why the colon :))? Why is the expression
enclosed in quotes ("")? Could you expand on this for me
or point me in the direction of some documentation.
Typing SET /? doesn't help me much.
Seems the colon is a typo. After repairing the broken link, I couldn't
see any occurence of that in the referenced thread.

The quoting of the set'ed argument is a tecnic to assure there is no
trailing content like spaces. IMO of no use in a set /A.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top