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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

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

#1 Post by BatMaster » 01 Oct 2011 06:09

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#2 Post by Ed Dyreen » 01 Oct 2011 06:19

'
[SOLVED] SET /a -- Random Number?
viewtopic.php?f=3&t=1817&hilit=random+number

Just don't get addicted to macros :lol:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#3 Post by aGerman » 01 Oct 2011 06:48

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

Post Reply