PowerShell: Generate Random Number Between Range


You can use the following methods in PowerShell to generate a random number between a specific range:

Method 1: Generate Random Integer Between Range

Get-Random -Minimum 1 -Maximum 10

This particular example will generate a random integer between 1 and 10 (not including 10).

Method 2: Generate Random Integer Between Range (Make it Reproducible)

Get-Random -Minimum 1 -Maximum 10 -SetSeed 55

By using -SetSeed we are able to max this example reproducible. In other words, the same random number between 1 and 10 (not including 10) will be generated each time we run this line.

Note that the number used after -SetSeed can be any number you’d like. We chose 55 as an arbitrary number.

Method 3: Generate Random Floating Point Number Between Range

Get-Random -Minimum 1.0 -Maximum 10.0

This particular example will generate a random floating point number between 1 and 10 (not including 10).

Note: When using the Get-Random cmdlet, the Minimum value is included in the possible values that could be generated while the Maximum value is not.

The following examples show how to use each method in practice.

Example 1: Generate Random Integer Between Range

We can use the following syntax to generate a random integer between 1 and 10 in PowerShell:

Get-Random -Minimum 1 -Maximum 10

The following screenshot shows how to use this syntax in practice to generate a random number between 1 and 10 several different times:

PowerShell generate random integer between range

From the output we can see that the following random integers were generated during each run:

  • 2
  • 2
  • 6
  • 2
  • 6
  • 4
  • 5

Note that each random integer is between 1 and 10.

Example 2: Generate Random Integer Between Range (Make it Reproducible)

We can use the following syntax to generate a random integer between 1 and 10 in PowerShell and ensure that the random number is the same each time we run it:

Get-Random -Minimum 1 -Maximum 10 -SetSeed 55

The following screenshot shows how to use this syntax in practice:

PowerShell generate random number with SetSeed

Notice that each time we run this line the random number turns out to be 2.

By using -SetSeed we are able to make this example reproducible.

Example 3: Generate Random Floating Point Number Between Range

We can use the following syntax to generate a random floating point number between 1 and 10 in PowerShell:

Get-Random -Minimum 1.0 -Maximum 10.0

The following screenshot shows how to use this syntax in practice to generate a random floating point number between 1 and 10 several different times:

PowerShell generate random floating point number between range

Notice that the random floating point number is between 1 and 10 each time.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Return Multiple Values from Function
PowerShell: How to Find Duplicate Values in Array
PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard

Leave a Reply