# Batch Help(randomization)?

Hello Im workin on a batch file and i would like to have help with with making Cmd randomize between two selected numbers Example: Random 1-10 Answer: 3

Hello Im workin on a batch file and i would like to have help with with making Cmd randomize between two selected numbers Example: Random 1-10 Answer: 3

active| newest | oldestSo you want 1-10 eh? You can do this by forcing batch to pick a number between 1-10, type this on text and save as .bat and incorporate it into your stuff.

Ok follow me on this! It may take around 1 second to 1 minute to generate the number because there are over 32000 possible answers so be patient! I tested that it works!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@echo off

:ans

set /a ran=%random%

if /i %ran% GTR 10 goto :ans

if /i %ran% LSS 1 goto :ans

echo Batch chose the number %ran%

pause

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you can see GTR and LSS mean that if it is greater than 10 or lower than 1 it will regenerate the number until it is between those limits! IPlease read this!

But thank you so much!!

OK so batch is not great at random numbers

the best and easiest way in my opinion is to do this set /a var=%random% %% max + min

however if you try to set your min to something greater than zero it will ignore the maximum number

to avoid this you can put it in a loop like this

:start

set /a var=%random% %% 9 + 5

if %var% gtr 9 go to start

echo %var%

So I used the set /a number=%random% %% 40 + 5 but basically if you don't use 1 as the min number the number will go beyond the max for example using Min1 Max5 does from 1 to 5 but using min5 max10 it goes like 7,9 then crosses the max number... The language of BAT isn't the best..

:: pick a random number between say 0 to 19 or 1 to 20

:: take the hundredths of a second 0 to 99 from current time

set eddie=%time:~-2%

:: modulus doesn't like numbers like 08, it thinks it's octal, so knock off the zero

set first=%eddie:~0,1%

if %first%==0 set eddie=%eddie:~1,1%

:: make number modulus 20 to keep range 0 to 19 , so 99 mod 20 = 19

:: if you want 1 to 20 change the modulus to 19 and add 1 to eddie

set /a eddie=(%eddie%%%20)

echo number=%eddie%

pause

set /a number=%random% % max + min

The reason it doesn't work in batch files is because of a simple over-look.

set /a number=%random% %% max + min

Or have you all forgot that in the for command you need double percent signs instead of singles?

random numbers easy

take the millseconds off %time% 0 to 99

then xor to get in your range

help

echo. space space space word space space word

echo word word

the spaces don't show why ?

how to cure it ?

will pick a random number between 0 and %youmaxnum% (replace %yourmaxnum% with a number like 10, or else

set/a yourmaxnum=10

)

Thank you, Padlock.

%RANDOM% - expands to a random decimal number between 0 and 32767.

To get a number within a more limited range, use the remainder operation:

set /a a=%random% % 10 + 1

will set %a% to a value from 1 to 10. This will not be perfectly distributed since 10 is not an even factor of 32768, but it should be close enough for anything you're doing in a batch file. If you need more sophistication, learn a real programming language.

("help cmd", and help on the other CMD commands, is also worth checking when trying to understand what batch files can and can't do.)

Yours deserved a best answer!