How do i make a random number in batch?

How do I make a random number (1 - 100) in a batch file

4 Answers

Relevance
  • Rolle
    Lv 4
    10 years ago
    Favorite Answer

    %random% will generate a random number 0-32767.

    To get 0-100 use following code:

    @echo off

    set /A r=%RANDOM% %% 100 + 1

    echo %r%

  • 10 years ago

    set rnumber=%random:~-2%

    That command will generate a random number between 0 and 99

  • Risa
    Lv 4
    4 years ago

    @echo off color 0a cls echo Cmd is generating a random number between 1 and 10 echo This may take a moment. . . :loop set num=%random% if /i %num% GTR 10 goto loop *I would use set /a instead of only set, because set /a sets numbers and equations. if /i %num% LSS 1 goto loop *Same thing goto finish :finish cls echo Cmd chose the number %num% pause >nul if /i %num%==1 goto goblin *Erase all the "if /i" and just type "if %num%==" if /i %num%==2 goto wolf if /i %num%==3 goto wolf if /i %num%==4 goto wolf if /i %num%==5 goto dragon if /i %num%==6 goto dragon if /i %num%==7 goto dragon if /i %num%==8 goto dragon if /i %num%==9 goto dragon if /i %num%==10 goto dragon pause :goblin echo A 1/1 Goblin creature is attacking you! pause goto exit :wold ----------------------------------------... You meant wolf but you have wold! Since you misspelled it, that's the big reason why it's not working! echo A 4/4 Giant Wolf creature is attacking you! pause goto exit :dragon echo A 99/99 Dragon creature is attacking you! pause goto exit :exit Below is my code, which is exactly like yours, just with the modifications I said above: @echo off color 0a cls echo Cmd is generating a random number between 1 and 10 echo This may take a moment. . . :loop set /a num=%random% if %num% GTR 10 goto loop if %num% LSS 1 goto loop goto finish :finish cls echo Cmd chose the number %num% pause >nul if %num%==1 goto goblin if %num%==2 goto wolf if %num%==3 goto wolf if %num%==4 goto wolf if %num%==5 goto dragon if %num%==6 goto dragon if %num%==7 goto dragon if %num%==8 goto dragon if %num%==9 goto dragon if %num%==10 goto dragon pause :goblin echo A 1/1 Goblin creature is attacking you! pause goto exit :wolf echo A 4/4 Giant Wolf creature is attacking you! pause goto exit :dragon echo A 99/99 Dragon creature is attacking you! pause goto exit :exit If you don't like it when batch says "Press any key to continue" when you have a pause command, type >nul. EX: pause>nul Hope it works for you!

  • Anonymous
    10 years ago

    I am not sure whether it can be done at all!

    .

    Source(s): TaZ
Still have questions? Get your answers by asking now.