1.

Solve : Random number between 2 other numbers?

Answer»

How could you make a BATCH that picks a random number between 2 other numbers? I know how to pick a number from like 0-10 but how can you pick a random number from 100-150?

Heres the code from 1-10 but again, how would you make it like 10-50 etc.

Code: [Select]echo off
:1
cls
set /a R=%random%%% 10 +1
echo %R%
pause >nul
goto 1

Thanks in advance
Ok so i'm heading toward the direction of solving it by experimenting...i know it's not the best method so please post if you have a better idea.

So far i have this... This picks a number from 10-25 but im still confused why it works O.o lol
Code: [Select]echo off
:1
set /a R=%random%%% 16 +10
echo %R%
pause >nul
cls
goto 1
Quote

This picks a number from 10-25 but im still confused why it works

It works because, in this line:

set /a R=%random%%% 16 +10

1. %random% selects a random number between 0 and 32767
2. %% 16 takes the remainder after dividing it by 16. This will be between 0 and 15
3. + 10 adds 10 to that giving a number between 10 and 25.

To get a random number between X and Y where X is the lowest number and X is the highest number ALLOWED, take %random% modulo ((Y-X)+1) and then add Y.

This means: (USING your numbers)

From the highest number (25), take away the lowest number (10).

25 - 10 = 15

To this, add 1

15 + 1 = 16

So you make set /a do this: %random% %% 16

The result will be between 0 and 15.

Now you add the lowest number (10)

This gives you a number between 10 and 25

Quote from: shanked on November 27, 2010, 12:59:51 AM
i know it's not the best method so please post if you have a better idea.

Given the limited tools available in batch scripting, it isn't bad. It is quick to run and is only one line of code. A strict mathematician might say that the results are pseudo-random. It depends what you want it for. It is OK for a batch game you are WRITING for a learning exercise or if you are playing around.







Quote from: Me
To get a random number between X and Y where X is the lowest number and X is the highest number allowed, take %random% modulo ((Y-X)+1) and then add Y.

Correction of typo:

To get a random number between X and Y where X is the lowest number and Y is the highest number allowed, take %random% modulo ((Y-X)+1) and then add Y.

Wow thanks a lot! I actually understand how it works now I know this has been resolved years ago.. but for those who is/are interested in alternate solution... here it is... (without much calculation of random numbers)


:start
echo off
set /p min_num= Enter minimum number (single digit only):
set /p max_num= Enter maximum number:
:begin
Rem rnum - min random number for single digit OUTPUT only
Rem runm2  - max random number for
   
   set rnum=%random:~1,1%
   set rnum2=%random:~1,1%

rem condition of two random single digit numbers.   
   if %rnum% lss %min_num% goto begin
   if %rnum% leq %min_num% set /a newnum=%rnum%+%rnum2%
   if %rnum% gtr %min_num% set newnum=%rnum%%rnum2%
   if %newnum% gtr %max_num% goto begin


echo %newnum%


Discussion

No Comment Found