Lottery Number Generator (6 - 49)

Submitted by Richard Sheppard on Wed, 26/04/2006 - 17:12

Code from a while back

I must have written this about five years ago, when I wanted to break away from using only Dreamweaver behaviours to produce the clever stuff in PHP.

With our new content management system, I've discovered that I can throw in snippets of PHP code that will execute. The best of both worlds, it would seem to me!

So below you will find six numbers to play in a Lotto 6-59 game (like the UK's National Lottery).

Tonight's winning numbers are....

Ball Number 1 is 15.

Ball Number 2 is 29.

Ball Number 3 is 52.

Ball Number 4 is 39.

Ball Number 5 is 3.

Ball Number 6 is 16.

Everytime you visit this page, you will have a different set of numbers. Don't believe me? Click here to refresh the page.

The code that generates this looks like this (with comments to help):

// initialise array
Array = array ();

// fill the array from 1 to 49, emulating the filling of the bin
for($count 1$count <= 49$count++){

 * First, we shuffle. Take the new first number and then
 * take it out of the array with array_shift set the bin
 * spinning again with the shuffle function.
 * Ultimately, we become millionaires. I get 10% of all
 * winnings if you use this code and win. ;-)

for($index 1$index <= 6$index++){
"<p>Ball Number <strong>$index</strong> is <strong>$</strong>. /p>

My friend Greg and I were examining the entertaining conversations regarding the shuffle() function at and Greg pointed out that my bit of code here doesn't quite satisfy the Fisher Yates shuffle requirements, but what did they know? Neither of them ever won the lottery, did they?

27 June 2008

I thought I'd better do a version for the EuroMillions:

EuroMillions example

Number 1 is 5.

Number 2 is 10.

Number 3 is 17.

Number 4 is 40.

Number 5 is 4.

Lucky Star 1 is 5.

Lucky Star 2 is 8.

Richard Sheppard

Wed, 26/04/2006 - 19:05

A friend just pointed out while I had the web applications caching turned on the numbers didn't change. For the time being, I've disabled the caching, just so I can show off.

Anonymous (not verified)

Fri, 20/08/2010 - 18:08

Thanks alot for this little snippet. Very helpful for my project.

Anonymous (not verified)

Fri, 08/10/2010 - 15:51

I WON 13 MILLION DOLLARS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.