PowerShell v3 in a Year Day 12 Get Random

Get-Random was one of those cmdlets I knew about, but, had never really looked at. This is precisely why I wanted to do this "in a Year" exercise...it forces me to learn about things I thought I knew but had never really examined. At any rate, Get-Random, as the help outlines, performs two distinct and deterministic functions: 1) gets a random number or 2) selects objects randomly from a collection. In the end, you will end up with a single item: a number or an collection member. Digging a little deeper than this, however, there are lots of ways to use Get-Random. I will explore a few of them, along with some interesting examples from the help.

As further noted in the help, "The Get-Random cmdlet gets a randomly selected number. If you submit a collection of objects to Get-Random, it gets one or more randomly selected objects from the collection.Without parameters or input, a Get-Random command returns a randomly selected 32-bit unsigned integer between 0 (zero) and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647)."

There are two parameter sets for Get-Random:

  • Get-Random [[-Maximum] <Object>] [-Minimum <Object>] [-SetSeed <Int32>] [<CommonParameters>]
  • Get-Random [-InputObject] <Object[]> [-Count <Int32>] [-SetSeed <Int32>] [<CommonParameters>]

Looking at the parameters one by one we have the following for the first parameter set. The first parameter set is the one which focuses on usage with numbers.
  • Maximum: indicates a random maximum value for the randomized selection. There are a lot of little subrules.
    • On a 32-bit computer, the default maximum is a 32-bit integer.
    • On a 64-bit computer, you can also enter a 64-bit integer.
    • The max must be greater than the min.
    • If the value of Maximum or Minimum is a floating-point number, Get-Random returns a randomly selected floating-point number. 
    • If the value of Minimum is a double (a floating-point number), the default value of Maximum is Double.MaxValue. Otherwise, the default value is Int32.MaxValue.
    • On a 64-bit computer, if the value of Minimum is a 32-bit integer, the  default value of Maximum is Int32.MaxValue. 
    • If the value of Minimum is a double (a floating-point number), the default value of Maximum is Double.MaxValue. Otherwise, the default value is Int64.MaxValue.
  • Minimum: as with the maximum value, there are a few things to keep in mind:
    • Enter a 32-bit integer or a double-precision floating-point number, or an object that can be converted to an integer or double, such as a numeric string ("100"). 
    • On a 64-bit computer, you can enter a 64-bit integer. 
    • The default value is 0 (zero).
    • The value of Minimum must be less than (not equal to) the value of Maximum. 
    • If the value of Maximum or Minimum is a floating-point number, Get-Random returns a randomly selected floating-point number.
  • SetSeed: Specifies a seed value for the random number generator. 
    • This seed value is used for the current command and for all subsequent Get-Random commands in the current session until you use SetSeed again or close the session. 
    • You cannot reset the seed to its default, clock-based value.
    • The SetSeed parameter is not required. 
    • By default, Get-Random uses the system clock to generate a seed value.
    • Because SetSeed results in non-random behavior, it is typically used only when trying to reproduce behavior, such as when debugging or analyzing a script that includes Get-Random commands.
The second parameter set focuses on collections. It allows you to randomly select an item at random. Below are the parameters for this second parameter set:

  • InputObject: Specifies a collection of objects. 
    • Get-Random gets randomly selected objects in random order from the collection.
    • Enter the objects, a variable that contains the objects, or a command or expression that gets the objects. 
    • You can also pipe a collection of objects to Get-Random.
  • Count: Determines how many objects are returned. 
    • The default is 1. 
    • If the value of Count exceeds the number of objects in the collection, Get-Random returns all of the objects in random order.
  • SetSeed: Specifies a seed value for the random number generator. 
    • This seed value is used for the current command and for all subsequent Get-Random commands in the current session until you use SetSeed again or close the session. 
    • You cannot reset the seed to its default, clock-based value.
    • The SetSeed parameter is not required. 
    • By default, Get-Random uses the system clock to generate a seed value.
    • Because SetSeed results in non-random behavior, it is typically used only when trying to reproduce behavior, such as when debugging or analyzing a script that includes Get-Random commands.
These two parameter sets allow you to work with the pair of options you need to take advantage of the cmdlet. Lets look at some examples:
  1. Get-Random, by itself, returns a number: 808892034.
  2. With -Maximum it returns a number between 0 and the number one less than the value of your maximum: Get-Random -Maximum 10 returns 4.
  3. When using -Maximum and -Minimum parameters, you get a number within the range specified by these values. Get-Random -Maximum 10 -Minimum 7 returns 8.
  4. Using floating-point numbers instead of integers causes the cmdlet to return a floating-point number. Calling Get-Random -Maximum 9.99 -Minimum 1.0 returns 5.80022345944784.
  5. If you want to select a random number from a collection you can:
    1. Pipe a collection to Get-Random: 1, 3, 5 | Get-Random returns 3.
    2. Pipe a collection to Get-Random: 1, 3, 5 | Get-Random -Count 2 returns 1, 5.
    3. Pipe a set of files to Get-Random to get files: dir C:windows | Get-Random -Count 10
  6. Use SetSeed to indicate a fixed value per session: Get-Random -SetSeed 10. This will return the same value so long as you keep the same session open.
While I dont personally have a need for randomized values often, I did learn a good bit about this cmdlet by reading through the help. Being able to pipe a collection to Get-Random is a useful way to run a PowerShell User Group book lottery. Not that anyone I know had done anything like that...no, never (http://import-powershell.blogspot.com/2012/03/prize-drawing.html).

Related Posts by Categories

0 comments:

Post a Comment