Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Random number between 2 other numbers  (Read 15843 times)

0 Members and 1 Guest are viewing this topic.

shanked

    Topic Starter


    Beginner
    • Experience: Familiar
    • OS: Windows Vista
    Random number between 2 other numbers
    « on: November 26, 2010, 11:18:27 PM »
    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 :)
    ...

    shanked

      Topic Starter


      Beginner
      • Experience: Familiar
      • OS: Windows Vista
      Re: Random number between 2 other numbers
      « Reply #1 on: November 27, 2010, 12:59:51 AM »
      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
      ...

      Salmon Trout

      • Guest
      Re: Random number between 2 other numbers
      « Reply #2 on: November 27, 2010, 02:24:48 AM »
      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

      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.








      Salmon Trout

      • Guest
      Re: Random number between 2 other numbers
      « Reply #3 on: November 27, 2010, 03:50:10 AM »
      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.


      shanked

        Topic Starter


        Beginner
        • Experience: Familiar
        • OS: Windows Vista
        Re: Random number between 2 other numbers
        « Reply #4 on: November 27, 2010, 12:37:16 PM »
        Wow thanks a lot! I actually understand how it works now :D
        ...

        homeflash



          Greenhorn

          Re: Random number between 2 other numbers
          « Reply #5 on: April 05, 2019, 07:36:46 AM »
          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%