user2507295
user2507295

Reputation: 159

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.

Upvotes: 3

Views: 6694

Answers (4)

LiamSmith Xl
LiamSmith Xl

Reputation: 1

Simple program that allows you to set the amount of numbers for an easy choice program...

:loop
cls
goto before

:before
@echo off
setlocal delayexpansion
cls
@mod con: clos=80 lines=25
title Number Picker
color 0a
cls
goto menu

:menu
cls
echo.
echo Number Picker
echo.
set/p input= "Number: "
if %input%== goto show

:show
set /a bottomlimit = 0
set /a upperlimit = %input%
set /a result = %bottomlimit% + %random% %% (%upperlimit% - %bottomlimit% +1)
echo.
echo Random Number: %result%
echo.
echo Press 'Y' to close press 'N' to go to menu...
echo.
set/p input= "Exit: "
if %input%==y exit
if %input%==n goto loop

Upvotes: 0

jared nolte
jared nolte

Reputation: 11

@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

Upvotes: 1

Gabor Orosz
Gabor Orosz

Reputation: 71

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%

Upvotes: 7

Nabeel Saad
Nabeel Saad

Reputation: 142

this should help:

# If you need a random int within a certain range, use the 'modulo' operator.
# This returns the remainder of a division operation.

RANGE=500

echo

number=$RANDOM
let "number %= $RANGE"
#           ^^
echo "Random number less than $RANGE  ---  $number"

#  If you need a random integer greater than a lower bound,
#+ then set up a test to discard all numbers below that.

FLOOR=200

number=0   #initialize
while [ "$number" -le $FLOOR ]
 do
  number=$RANDOM
done
echo "Random number greater than $FLOOR ---  $number"
echo

# Let's examine a simple alternative to the above loop, namely
#       let "number = $RANDOM + $FLOOR"
# That would eliminate the while-loop and run faster.
# But, there might be a problem with that. What is it?

# Combine above two techniques to retrieve random number between two limits.
number=0   #initialize
while [ "$number" -le $FLOOR ]
  do
    number=$RANDOM
    let "number %= $RANGE"  # Scales $number down within $RANGE.
  done
echo "Random number between $FLOOR and $RANGE ---  $number"

Taken from here: http://tldp.org/LDP/abs/html/randomvar.html

Then, once you have the $number, you can just do something like: num=expr $number + $MAINSCORE

Hope that helps.

Upvotes: 0

Related Questions