# Dreaming In PowerShell V2 : Lottery Numbers with Get-Random

### Dreaming In PowerShell V2 : Lottery Numbers with Get-Random

Tobias Weltner writes a blog called Dreaming In PowerShell, and he recently posted a way to get a list of unique lottery numbers with PowerShell.  Dreaming In PowerShell is a cool blog, and the post is interesting, but it makes an assertion that's no longer true in V2.  He uses System.Random to create the random numbers because he asserts that there isn't a cmdlet to Get random numbers.  Get-Random is a V2 cmdlet that can not only give you random numbers, but can give you unique random numbers within a range

My CTP3 version is on the left, Tobias' V1 version is on the right:

 My CTP3 Version Tobias' V1 version: ```function Get-RandomNumbers(\$minimum = 1, \$maximum = 49, \$number = 20) { #.Synopsis # Gets a series of unique random numbers #.Description # Gets a series of unique random numbers between a minimum and a maximum #.Parameter number # The number of unique numbers needed #.Parameter minimum # The lowest number the random numbers could contain #.Parameter maximum # The highest number the random numbers could contain #.Link # Get-Random #.Example # Get-RandomNumbers 10 1 100 \$minimum..\$maximum | Get-Random -Count \$number | Sort-Object }``` ```function Get-RandomNumbers(\$minimum = 1, \$maximum = 49, \$number = 20) { \$random = New-Object System.Random \$result = @() do { \$randomnumber = \$random.Next(\$minimum,\$maximum) if (\$result -notcontains \$randomnumber) { \$result += \$randomnumber } } while (\$result.count -lt \$number) \$result = \$result | Sort-Object \$result }```

The core function is a lot shorter (3 lines in CTP3 to 10 lines in V1), but my CTP3 version has inline help.  This means not only can I easily Get unique random numbers, I can also get help about getting unique random numbers.

While I'm showing Get-Random with a bunch of numbers, you can easily use the same thing to select from a list of words.  Check out this pipeline that gives you 5 random verbs to explore

```Get-Command -type Cmdlet |
Group-Object Verb |
Foreach-Object { \$_.Name } |
Get-Random -count 5```

Isn't CTP3 Fun?

Hope this Helps,

James Brundage [MSFT]

• Post
• There is just one small problem with the new scripts - it does not check for duplicate numbers. Since the numbers are "random", there's some probability that the script could produce two numbers the same. While this would be "random", it would not be a winning lottery ticket since you never get duplicate numbers from a lottery draw.

So while Tobias' script is longer, I have a somewhat better chance of winning the lottery using his results.

Then again, the lottery is just a tax on people who can't do maths.

• Actually, Get-Random DOES check for duplicates.  This also avoids a problem that Tobias' script has, which is that if you ask for more random numbers than exist in the range, you get an infinite loop

The easiest proof of this is:

1..2 | Get-Random -count 5 | Measure-Object

Count    : 2

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

• The moment I sent the earlier post, I realised just what -count actually does and how this syntax variant is used - I'm wrong!

This is a really cool feature (now that I actually understand it).

Page 1 of 1 (3 items)