CMD Random Number Generator W/ Range V1

Posted: February 1, 2011 in CLI, Windows

Hello all,

This miniature batch is just a random number generator that works between 0 and 32767.
What is does.
Takes 2 inputs %1 (Minimum Number) and %2 (Maximum Number)
Then it runs %random%  operation until the returns a number between the range you specified, there are 2 if´s that work as limiter to ensure the batch does not loop.
Please do remember that if you create a batch and want to run it as a command you have to put it in “c:/windows/system32/” and named as command.bat this will allow you to run the command in CMD, this Random Number Generator runs like this.
Note:  Low ranges may take more time as the chance to get a number in that range is lower.



@echo off
set minimum=%1
set maximum=%2
if %maximum% LEQ %minimum% (
echo Maximum is lower than Minimum
goto B
if %maximum% GEQ 32767 (
echo Maximum number is to big please use 32767 or below)
set randnumber=%random%
if %randnumber% GEQ %minimum% (
if %randnumber% LEQ %maximum% (
echo %randnumber%
goto END
goto A

My plan is to make an more interactive version, but not for now, I just needed a quick random command.
Remember that necessity always gives us a chance to create.

Will San

  1. M4TR1X_PRO says:

    – – Allows the user to pic there Range —

    @echo off
    title Random Number Generator
    color 0a
    set /p L=Low:
    set /p H=High:
    echo Cmd is generating a random number between %L% and %H%
    echo This may take a moment. . .
    set num=%random%
    if /i %num% GTR %H% goto loop
    if /i %num% LSS %L% goto loop
    goto finish
    color 0c
    echo Cmd chose the number %num%
    pause >nul
    goto A

  2. KGerő Sork says:

    Echo Off
    Set Rand=%Random%
    Set /A Rand=%Rand%*(%2-%1)/32768+%1
    Echo %Rand%

  3. D.O.S says:

    “if %maximum% GEQ 32767 (
    echo Maximum number is to big please use 32767 or below)”

    There are 3 issues with this piece of code:
    1. If a user enters 32767, the GEQ will tell them to enter 32767 or below, to fix this use GTR.
    2. Wrong use of to, it should be too. [e.g. too much cake].
    3. The echo sentence requires some punctuation; you appear to have 2 sentences merged into 1.

    so the corrected statement would be:
    if %maximum% GTR 32767 (
    echo Maximum number is too big. Please use 32767 or below.)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s