Simple batch file help

Acer / Aspire 7720
January 18, 2011 at 20:13:26
Specs: Windows Vista, 1.5 GHz / 2037 MB
I am creating a batch file with the MS-DOS in Windows Vista and I need the file to generate a random number between 1 and 10 and put it in a variable. I've tried putting limits on the %RANDOM% command, but I've had no success in figuring it out. Any help would be appreciated.

See More: Simple batch file help

Report •


#1
January 18, 2011 at 21:12:30
Modulo is probably the best answer.

set /a var=(%random% %% 9) + 1
echo %var%

Here is a range version:

rem max must be geq min
set min=1
set max=9
set /a var=(%random% %% ((max - min) + 1 )) + min
echo %var%


Report •

#2
January 19, 2011 at 01:33:43
Hi Judago,

How 'bout mod 10 + 1?


=====================================
Life is too important to be taken seriously.

M2


Report •

#3
January 19, 2011 at 02:41:48
Hi M2,

I figured it's more likely that 1 - 9 was actually wanted(I could be wrong).


If you take the op literally it's more like 2 - 9.........


Report •

Related Solutions

#4
January 19, 2011 at 07:01:49
Lazy way:
SET someNum=%RANDOM:~-1%
SET /A someNum += 1

How To Ask Questions The Smart Way


Report •

#5
January 19, 2011 at 15:53:21
Even Lazier way ;)

SET /A someNum=%RANDOM:~-1% + 1


Report •


Ask Question