Page 1 of 1

use %random% to echo a number between 200 and 400

Posted: 01 Oct 2011 06:09
by BatMaster
hi
I was wondering if you can tell the %random% command to generate a number between 200 and 400
any help would be greatly appreiciated
-=BatMaster=-

Re: use %random% to echo a number between 200 and 400

Posted: 01 Oct 2011 06:19
by Ed Dyreen
'
[SOLVED] SET /a -- Random Number?
viewtopic.php?f=3&t=1817&hilit=random+number

Just don't get addicted to macros :lol:

Re: use %random% to echo a number between 200 and 400

Posted: 01 Oct 2011 06:48
by aGerman
Ed Dyreen wrote:'
Just don't get addicted to macros :lol:

I'm afraid it's difficult to understand for a beginner :|

I'll try to explain how such a calculation could look like:

Code: Select all

@echo off &setlocal
set /a n1=200
set /a n2=400

:: difference between n1 and n2
set /a n3=n2-n1

:: check whether n1 was less than n2
if %n3% lss 0 (set /a n3*=-1, n1=n2)

:: %random% contains a number between 0 and 32767
:: the modulo (%%) operation calculates the remainder of division %random%/(n3 + 1)
:: the result is a number between 0 and n3, now add n1 to the remainder
set /a rdm=%random% %% (n3 + 1) + n1

echo %rdm%
pause

Regards
aGerman