PowerShell: How to Generate a Random Number?

Are you looking for a way to generate random numbers in PowerShell? There could be countless reasons why you may need to generate a random number in PowerShell, including testing data, Randomization, Simulation, statistical analysis, simulating real-world scenarios, generating unique identifiers, etc. This blog post will discuss the steps needed to generate random numbers with PowerShell, along with some PowerShell script examples. Let’s explore the different ways you can generate random numbers in PowerShell!

How to Generate a Random Number in PowerShell?

In computing, a random number typically refers to a number that can’t be deterministically predicted. To generate a random number in PowerShell, you can use the Get-Random cmdlet.

PowerShell comes with its native function for generating random numbers using the Get-Random cmdlet, which adds powerful functionality to your automation scripts without having to write complex algorithms or search online for third-party modules.

# Generate a random number
$RandomNumber = Get-Random

This will generate a random number between the 32-bit integer 0 and 2147483647, and $RandomNumber will now contain a random integer.

Getting a Random Floating Point Number

By default, The Get-Random method generates integer numbers. If you want to generate a random floating-point number, just provide the -Minimum or -Maximum parameter (or both) in decimal. E.g.

Get-Random -Minimum 1.5
Get-Random -Maximum 10.0

Assigning a Random Number to a Variable

Often, you may want to assign a random number to a variable for further processing or manipulation. You can store a generated random number in a variable to reuse it later, similar to any other value in PowerShell:

$RandomNum = Get-Random -Maximum 100
Write-Output "Random number is: $randomNum"

Then, you can use the $randomNum variable anywhere later in your script. You can then use this variable in your script as needed. By assigning a random number to a variable, you can perform various operations, such as comparing, sorting, or using it in conditional statements.

Generate a random number within range in PowerShell

If you would like to generate a random number within a specific range, you can use the -Minimum and -Maximum parameters to specify a range for the random number. Here’s an example of how you can use it to generate a random integer between a specific range:

$RandomNumber = Get-Random -Minimum 1 -Maximum 10

This will assign a random integer between 1 and 9 (Not 10! Always set one number higher in maximum) to the $RandomNumber variable. Need a 4-digit random number? Sure! To get a random 4-digit number in PowerShell, set the range between 1000 and 9999:

# Get a Random 4 Digit Number
$FourDigitNumber = Get-Random -Minimum 1000 -Maximum 10000

Generating a random 4-digit number is a common requirement in many scenarios, such as generating PIN codes or random identifiers. Need a six-digit random number? No issues!

Get-Random –Minimum 000000 -Maximum 999999

Here is another example to generate a random number between -10 and 10:

$min = -10
$max = 10
Get-Random -Minimum $min -Maximum $max

Get Random from Array in PowerShell

You can also use the -InputObject parameter to specify an array from which the random number should be chosen. For example:

$RandomNumber = Get-Random -InputObject 1,2,3,4,5,6,7,8,9,10

You can also pass in an array of integers as arguments to Get-Random; this will return one of the integers from the array at random. For example:

# create an array with values 1 through 5
$Values = @(1, 2 ,3 ,4 ,5)

# Get a random value from $values at random
Get-Random -InputObject $Values

This will generate a random element from the given array. Similarly, if you want to select an item from an array of strings randomly:

#Create an array of strings
$Array = @('Apple','Banana','Orange','Pear')

# Get an array of strings
$Random = Get-Random -InputObject $Array

#Get-Random cmdlet accepts the input object from Pipeline as well
$Array | Get-Random
PowerShell Generate Random Number

Here is a real-world example: Let’s pick a random file from the given folder.

$FolderPath = "C:\Temp"
$Files = Get-ChildItem $FolderPath -Recurse -File
$RandomFile = $Files | Get-Random
$RandomFile.Name

In this example, we specify the path to a folder and get all the files within that folder. We then use the Get-Random cmdlet to select a random file from the list of files and display its name.

Generate Multiple Random Numbers

To generate multiple numbers at once, use the -Count parameter:

Get-Random -InputObject 1, 2, 3, 4, 5 -Count 2

Here is another way to generate multiple random numbers:

1..5 | ForEach-Object { Get-Random -Minimum 1 -Maximum 10 } | Sort -Unique

Conclusion

In this guide, we covered the core methods for generating random numbers and values in PowerShell. The key cmdlet is Get-Random, which can produce random integers, decimals, and even random indices for strings. As we have seen above, these random number generating techniques in PowerShell can be used for various purposes, such as selecting a random item from arrays or performing other tasks where randomized results are needed. With just a few lines of code, you can easily create randomly generated numbers, both integers and floats.

To get a random string in PowerShell, such as a Random password, refer: How to Generate a random password in PowerShell?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *