Generate random numbers on a Microsoft Windows system

If you need to produce a random number on a Microsoft Windows system, you can do so at a command line interface (CLI) or in a batch file using the %RANDOM% environment variable - see Windows Environment Variables for a list of the environment variables available on Microsoft Windows systems.

To generate random numbers at the command line, you can open a command prompt window and type echo %random%.

c:\Users\Public\Documents>echo %random%
31090

c:\Users\Public\Documents>echo %random%
16802

c:\Users\Public\Documents>echo %random%
20253

c:\Users\Public\Documents>

When you use %random%, you will get numbers between 0 and 32,767, which is 2 to the 15th power. But you can make the range of numbers smaller by using a command like the following where 0 is the lower bound for the numbers and n-1 is the upper bound of the range.

SET /a _rand=(%RANDOM%*n/32768)

The SET command can be used to display, set, or remove CMD environment variables. SET /a will always round down in arithmetic calculations.

E.g., if you wanted the range of numbers to be between 0 and 99, you could use the following:

set /a _rand=(%random%*100/32768)

If you wanted the range to be from 1 to 100, you could use the following command, instead:

c:\Users\Public\Documents>set /a _rand=(%random%*100/32768) + 1
80
c:\Users\Public\Documents>set /a _rand=(%random%*100/32768) + 1
7
c:\Users\Public\Documents>set /a _rand=(%random%*100/32768) + 1
43

If you wanted to generate ten random numbers between 0 and 32,767, you could use a FOR /L command. The syntax for the command is FOR /L %%parameter IN (start,step,end) DO command. E.g.:

@echo off
Setlocal EnableDelayedExpansion

for /l %%i in (1,1,10) do echo !random!

You must use setlocal EnableDelayedExpansion and put exclamation marks rather than percent signs around the variable, because otherwise Windows will interpret the variable only once and not for each iteration of the loop as shown below:

DJI Phantom 3 Drone
c:\Users\Public\Documents>for /l %i in (1,1,10) do echo %random%

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

c:\Users\Public\Documents>echo 20737
20737

Note: you use only one percent sign for a loop variable, e.g., %i, when using the variable in a command issued at the command prompt, but you must use two percent signs, e.g., %%i, for the loop variable in a batch file.

If I wanted to generate ten random numbers between zero and ten, I could use the following commands in a batch file:

@echo off
Setlocal EnableDelayedExpansion

for /l %%i in (1,1,10) do (
   set /a _rand=!random!*10/32768
   echo !_rand!
)

Bear in mind the following information from Random Numbers, though:

The distribution of numbers returned will be a determined by both the range and the quantity of numbers drawn.

For example if you are drawing random integer numbers where each number is between 0 and 100 then on average:

The output of a few runs of a batch file containing the above commands is shown below:

Generic Category (English)120x600

c:\Users\Public\Documents>randnums
0
7
1
0
6
9
3
4
2
0

c:\Users\Public\Documents>randnums
4
4
1
4
8
8
5
1
0
3

c:\Users\Public\Documents>randnums
2
1
2
5
9
2
1
6
6
3

c:\Users\Public\Documents>randnums
4
3
3
5
6
0
1
6
5
4

c:\Users\Public\Documents>randnums
8
0
2
5
3
4
1
4
2
1

c:\Users\Public\Documents>randnums
3
6
2
3
6
2
9
6
1
8

c:\Users\Public\Documents>randnums
5
0
4
4
5
1
8
3
3
7

c:\Users\Public\Documents>

Alternatively, you can use the Get-Random cmdlet from a Windows PowerShell prompt, instead of the command prompt. On a Windows 10 system, you can type PowerShell in the "Ask me anything" field and then click on "Windows PowerShell" when you see it returned as a match to get a PowerShell prompt.

Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS C:\Users\nell> Get-Random
1451408813
PS C:\Users\nell>

You can specify a range of numbers by using the minimum and maximum parameters for the command. The maximum number should always be one greater than the maximum number you wish to have returned, e.g., if you want numbers returned between 1 and 10 you could use a command like the one shown below:

oreilly.com - Your tech ebook super store
PS C:\Users\nell> Get-Random
1451408813
PS C:\Users\nell> Get-Random -minimum 1 -maximum 11
9
PS C:\Users\nell> Get-Random -minimum 1 -maximum 11
9
PS C:\Users\nell> Get-Random -minimum 1 -maximum 11
7
PS C:\Users\nell> Get-Random -minimum 1 -maximum 11
8
PS C:\Users\nell> Get-Random -minimum 1 -maximum 11
3
PS C:\Users\nell>

You can create a PowerShell script that will execute the command a specified number of times. E.g., a script containing the following commands would generate 10 random numbers between 1 and 10. The 1..10 indicates the loop should be executed 10 times.

1..10 | ForEach-Object -process {
   Get-Random -minimum 1 -maximum 11

You can run the script from a PowerShell prompt using the command below, if the script was named randnums.ps1.

& "C:\Users\public\documents\randnums.ps1"

If you haven't executed PowerShell scripts on the system before, you will likely see the message below, however.

PS C:\Users\public\documents> & "C:\Users\public\documents\randnums.ps1"
& : File C:\Users\public\documents\randnums.ps1 cannot be loaded because running scripts is disabled on this system.
For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ & "C:\Users\public\documents\randnums.ps1"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS C:\Users\public\documents>

You will need to change the execution policy for scripts on the system in that case as explained at How to Write Your First Powershell Script. To change the execution policy you can type PowerShell in the "Ask me anything" field on a Windows 10 system, then when you see Windows PowerShell appear, right-click on it and choose Run as administrator. When asked if you wish to allow the app to make changes to the system, click on Yes. Then at the PowerShell prompt type Set-ExecutionPolicy RemoteSigned .

Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
PS C:\Windows\system32>

You can then go back to the other PowerShell window and execute the script.

PS C:\Users\public\documents> & "C:\Users\public\documents\randnums.ps1"
7
4
6
1
6
9
4
1
7
8
PS C:\Users\public\documents>

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px