Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMD-Batch File Simple Random

Using a .bat file, I know how to echo out a random number with %Random%. How do I set a certain random range of the %Random% e.g. 50-100? Oh, and I've got a point system which states at the start: SET /A MAINSCORE=0 How do I set that Random Range number to add / subtract from the MainScore? Thanks.

like image 576
user2507295 Avatar asked Jul 24 '26 14:07

user2507295


2 Answers

The %RANDOM% returns a number between 0 and 32767. To narrow these range use modulo operator, and addition or substraction to offset the result. Example:

@set /a bottomlimit = 50
@set /a upperlimit = 100
@set /a result = %bottomlimit% + %RANDOM% %% (%upperlimit% - %bottomlimit% + 1)
@echo %result%
like image 119
Gabor Orosz Avatar answered Jul 26 '26 04:07

Gabor Orosz


@echo off
set min=5
set max=10
set /a range=max-min +1
set /a rnd=%random% %%%range% +%min%
echo %rnd%
pause

personally i really like my way of doing it
this will go through any # between 5 and 10
and best of all, its clean. thank you

like image 29
jared nolte Avatar answered Jul 26 '26 04:07

jared nolte