Home > Development Tools, Windows > How to generate random numbers in a batch file?

How to generate random numbers in a batch file?

Ever wondered how we can generate random numbers in a batch file. Yes, I am talking about our good old faithful friend ‘.bat’ file. Why do I need one? You may ask! I had to generate a unique filename everytime because I had to backup a folder before I updated it.

So tell us is there one? Yes use ‘random’, it’s a built in command which generates a random number when you invoke it. Every time a unique one! So if you type in ‘random’ at a command line and then press enter, you’ll get an error because it’s not an independent executable, but a feature that is built into command prompt to help generate random numbers. So how will I get a random number? This is how I do it…

echo %random%

Make a random directory….

mkdir C:\Nibu_%random%

If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don’t show up in the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override dynamic ones like %random%.

For more information type “set /?”

To enable command extensions open up regedit and then navigate to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\EnableExtensions
or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions

and set value to 1. If not present then add one REG_DWORD value with an identical name and value set to 1.

Advertisements
  1. Monty
    June 4, 2010 at 10:43 pm

    echo off
    cls
    echo –
    set /p NumOfChoices=Enter number of choices…
    echo – –
    set /a Divisor=%NumOfChoices%/2
    echo %NumOfChoices%
    echo – – –
    echo – Ready to start spewing random numbers as soon as you
    echo –
    pause

    :loop
    title Ctrl-C to stop this batch file
    set /a randoom=%Random%
    set /a ThaRandomNum1=%randoom%/%Divisor%
    set /a ThaRandomNum2=%ThaRandomNum1%+1
    echo %ThaRandomNum2%
    goto loop

  2. February 15, 2010 at 5:16 pm

    Thanks, helped me out in one of my projects.

  3. Interociter Operator
    October 10, 2009 at 6:47 am

    maybe….

    set /a (%random%/1638)+1

    Divide 32768 by whatever number of choices you need, then use that number with the random variable, adding 1 so that it rounds up to your highest numbered choice.
    ——–
    @echo off
    cls
    echo –
    set /p NumOfChoices=Enter number of choices…
    echo – –
    set /a Divisor=32768/NumOfChoices
    echo – – –
    echo – Ready to start spewing random numbers as soon as you
    echo –
    pause
    :loop
    title Ctrl-C to stop this batch file
    set /a ThaRandomNum=(%random%/%Divisor%) + 1
    echo %ThaRandomNum%
    goto loop

  4. Interociter Operator
    October 10, 2009 at 6:21 am

    That will give us random numbers from 1 to 32768.
    What if we want random numbers from 1 to 20?

  5. November 23, 2008 at 12:51 pm

    Thanks Anand.

  6. November 23, 2008 at 11:44 am

    Nice blog. Good stuff here 🙂

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: