How to generate Lottery Numbers

Summary

Lottery numbers can be generated with PowerShell, using this line (this is just one way – there are others):

1..3 | % {“Lottery Line: $_”; 1..1000 | % { get-random -Minimum 1 -Maximum 49 } | select -Unique | select -First 6 | Sort}

1..3 | % {"Lottery Line: $_"; 1..1000 | % { get-random -Minimum 1 -Maximum 49 } | select -Unique | select -First 6 | Sort}

If you don’t know how to start PowerShell, click here to understand that, first.

The screen immediately below is the National Lottery number confirmation screen: it is not for entering data. If you are thinking either a) that’s obvious or b) well, that’s confusing – a data entry screen is exactly what I thought it was, click here.

Lottery02

Detail

… that obscures the elegance of the summary. Ignore this if the summary gave you what you needed.
I don’t want to use the same set of numbers that I use all the time, and I don’t want to do Lucky Dip. I want the illusion that somehow I control the numbers… but don’t want to think about it. I need a script that:

  • Generates random numbers between 1 and 49 (for the UK National Lottery).
  • Allows me to specify any number of lottery lines

The PowerShell script

As I said, ignore this if not of interest.

A couple of ways to generate the numbers – first using the .Net Random object:

$rand = New-Object System.Random(65000)
$lotteryArray = @()
1..1000 | % {$lotteryArray += $rand.Next(1,49)}
$lotteryArray | select -Unique | select -First 6

ExampleRandomNumbers01

Another way is to use PowerShell’s Get-Random. In fact this does it all in 1 line, so just use this (but I’m keeping the other one in for those interested):

1..1000 | % { get-random -Minimum 1 -Maximum 49 } | select -Unique | select -First 6

lotto02

And as using this approach, we’re doing it all on 1 line, now let’s say how many lottery lines we want to generate – so in the example below, the 3 reference in the 1..3 bit says how many lottery lines (do this all on one line):

1..3 | % {"Lottery Line: $_"; 1..1000 | % { get-random -Minimum 1 -Maximum 49 } | select -Unique | select -First 6}

That then gives you:

lotto01

 

And then we plug that into the National Lottery numbers:

Lottery02

Usability

My wife, who with my daughter is one half of my Usability Team™, pulled me on the fact that the pretty screen looks like a dialogue where you can actually click and enter numbers. Nah, it’s just to break up the monotony of the text. She couldn’t believe that in 2014, we would actually have a new thing that didn’t have a Graphical User Interface. Well, we do, and in the Microsoft world, it’s getting bigger with each release of Windows.

Starting PowerShell

Your PC (not Mac, or tablet) will have PowerShell. To start it, you…
Windows 7: in the Start box, start typing PowerShell. It will show a number of choices. Pick PowerShell.exe
Windows 8: on the Start screen, start typing PowerShell. It will show a number of choices. Pick Windows PowerShell

 

Advertisements