Lottery Number Generator (6 - 49)

Submitted by Richard Sheppard on Wed, 2006-04-26 17:12. Posted in  | 

»

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-49 game (like the UK's National Lottery).

Tonight's winning numbers are....

Ball Number 1 is 7.

Ball Number 2 is 3.

Ball Number 3 is 22.

Ball Number 4 is 26.

Ball Number 5 is 28.

Ball Number 6 is 2.

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):

<?php 
/* initialise array */
$lottoNum = array ();

/* fill the array from 1 to 49, emulating the filling of the bin */
for($counter 1$counter <=49$counter++)
    {
        
array_push($lottoNum,$counter);
    }
    
/*    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++)
    {
/* set the bin rolling with the shuffle function */
        
shuffle($lottoNum);
        print(
"<p>Lotto Number <strong>$index</strong> is <strong>$lottoNum[0]</strong>.</p>\n");
        
array_shift($lottoNum);
    }
?>

My friend Greg and I were examining the entertaining conversations regarding the shuffle() function at http://www.php.net/manual/en/function.shuffle.php 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 Euro Lottery:

Euro Number 1 is 26.

Euro Number 2 is 31.

Euro Number 3 is 11.

Euro Number 4 is 32.

Euro Number 5 is 40.

Euro Star 1 is 6.

Euro Star 2 is 7.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Wed, 2006-04-26 19:05

Numbers might not change

Richard Sheppard Says:

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.

»

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

Add image
  • Avast! This website be taken over by pirates on September 19th. Yarr!
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p><img><blockquote><h1><h2><h3><h4><h5><h6>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This is to prevent automated internet bots from doing naughty things.