Playing the lottery

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Playing the lottery

Post by bokehman »

Hola amigos! I created a lottery class for fun but it has a problem. What it does is choose 6 random numbers between 1 and 49. It then plays those numbers every week until they win the jackpot. It then returns the date on which the numbers won. In theory there are 15,890,700 possible combinations but when I run the script it normal hits the jackpot on average after only 500,000 random numbers. What's wrong? Is the problem with the code or with PHP?

Code: Select all

<?php

class lottery
{
	function lottery()
	{
		static $seed = true;
		if($seed)
		{
			srand((double)microtime()*1000000 );
			$seed = false;
		}
		for($i = 1; $i < 50; $i++)
		{
			$this->numbers[] = $i;
		}
	}
	
	function gen_numbers($pulls)
	{
		for($i = 0; $i < $pulls; $i++)
		{
			$this->pulls[] = $this->pull();
		}
	}
	
	function pull()
	{
		shuffle($this->numbers);
		$pull = array_slice($this->numbers, 0, 6);
		sort($pull);
		return $pull;
	}
	
	function get_weeks()
	{
		$this->weeks = 1;
		while(($this->pull = $this->pull()) and (!in_array($this->pull, $this->pulls)))
		{
			$this->weeks++;
		}
		return $this->weeks;
	}
	
	function result()
	{
		$output = 'After '.($c = count($this->pulls)).' attempt'.(($c == 1)?null:'s').' per week your numbers ('.implode(', ', $this->pull).') came up on ';
		$ts = (($this->weeks % 52.1785)*7*24*60*60) + time();
		$output .= date('F jS ', $ts) . floor(date('Y', $ts) + ($this->weeks / 52.1785));
		return $output;
	}
} 

$bad_news = new lottery;
$bad_news->gen_numbers(1);
echo $bad_news->get_weeks().' weeks<br>';
echo $bad_news->result();

?>
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

First Run:

Code: Select all

98304 weeks
After 1 attempt per week your numbers (4, 5, 13, 14, 31, 42) came up on March 30th 3890
Second Run:

Code: Select all

473088 weeks
After 1 attempt per week your numbers (9, 12, 25, 26, 35, 40) came up on August 17th 11073
Third Run:

Code: Select all

348160 weeks
After 1 attempt per week your numbers (12, 19, 20, 32, 35, 43) came up on March 2nd 8679
Unfortunately (the reason why i have yet to win), it seems like it's working fine :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

nickvd wrote:it seems like it's working fine
Your reply demonstrates the problem. If there are 15 million combinations, averaged over several runs the numbers should come up at about iteration 7.5 million but they are coming up regularly at less than half a million iterations. Why is this happening?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Re: third run... if i started today, playing weekly, i'll hit my numbers in just roughly SEVEN THOUSAND YEARS... I dont see the problem (aside from the chances of winning being nil)

Or am I not understanding probability...
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

The problem is there are nearly 16 million combinations but it always finds the winning number with less than half a million attempts. How is that possible?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I guess shuffle() applied to the same array again and again isn't as random as expected.
Maybe array_rand() gives "better" results.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

bokehman wrote:The problem is there are nearly 16 million combinations but it always finds the winning number with less than half a million attempts. How is that possible?
Okay, I understand what you're getting at... Volka may just have it right. Keep in mind that Engineers have spent millions upon millions of dollars to try to come up with a true random number generator, so your problem is probably just related to the "pseudo-randomness" that php generates...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would much rather rely on probability equations than using php silly random generation engine ;)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Jcart wrote:I would much rather rely on probability equations than using php silly random generation engine ;)
How would you do that? I'm a bit rubbish at maths.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Jcart wrote:I would much rather rely on probability equations than using php silly random generation engine ;)
If I'm not mistaken, the probability of one lottery win (49 choose 6) for 500,000 tries is roughly 0.0013.
However, I haven't run the script yet and I guess we'll need more than a handful of tries to say anything specific.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

aerodromoi wrote:If I'm not mistaken, the probability of one lottery win (49 choose 6) for 500,000 tries is roughly 0.0013.
Hahaha, you wish. You can choose any number from 1 to 49 if I'm right. So you have 49 possibilities which mean that the probability to choose one right number is 1 to 49. Now, you need to guess 6 right numbers and there is no connection between each number to the other - each number is picked randomly. Therefore, the probability to guess all the 6 numbers and win the lottery is: (1 / 49)^6 = 7.22476158 × 10^(-11) :P
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Mine timed out after 60 seconds. I don't think it completed within that time. How do I disable timeout?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Oren wrote:
aerodromoi wrote:If I'm not mistaken, the probability of one lottery win (49 choose 6) for 500,000 tries is roughly 0.0013.
Hahaha, you wish. You can choose any number from 1 to 49 if I'm right. So you have 49 possibilities which mean that the probability to choose one right number is 1 to 49. Now, you need to guess 6 right numbers and there is no connection between each number to the other - each number is picked randomly. Therefore, the probability to guess all the 6 numbers and win the lottery is: (1 / 49)^6 = 7.22476158 × 10^(-11) :P
This would imply that you can choose one number more than once (up to six times, actually).
The last time I looked, there was only one cross per number ;)

As far as I know, the total number of ways of selecting k objects from a set of n is calculated with the binomial coefficient.

So the chance of a lottery win (one try) should be: [tex]\frac{1}{\binom{49}{6}}[/tex] or 1/13,983,816 .

However, if you enter the lottery with 500 000 tickets, you'll have 500 000 independent tries with the above mentioned probability.
Which leads imho to a probability of ~0.0345 for a single lottery win (for 500000 drawings) (I muddled up the first prob over comma vs. point notation :().
Image
[edit for clarification1]
This means 500 000 tries with one ticket per week/drawing.

Going even further, I'd say that you need roughly 42 million tickets to win the lottery (at least once) with a probability of >= 95%.
[edit for clarification2]
This means 42 million tries with one ticket per week/drawing. As volka correctly points out,
14 million tickets for one drawing might be a better idea. ;)

Image
Last edited by aerodromoi on Sat Oct 14, 2006 9:02 am, edited 2 times in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Going even further, I'd say that you need roughly 42 million tickets to win the lottery (at least once) with a probability of >= 95%.
hm, in this case I'd rather play only once with 13.983.816 tickets - each with another combination boosting my chance to "win" to 100% ;)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

aerodromoi wrote:As far as I know, the total number of ways of selecting k objects from a set of n is calculated with the binomial coefficient.
That's al well and good but that gets the possibilities only. In PHP the following code does that:

Code: Select all

<?php

echo '<p>The number of possible lottery combinations of 6 numbers chosen from numbers between 1 and 49 is: ';
echo '<b>'.number_format(CombinationsWithoutRepetition(49, 6)).'</b>';

echo '<p>The of possible 5 card poker hand combinations possible to make from a 52 card deck is: ';
echo '<b>'.number_format(CombinationsWithoutRepetition(52, 5)).'</b>';

function CombinationsWithoutRepetition($total, $quantity)
{
	return((factorial($total))/(factorial($quantity)*factorial($total-$quantity))).'</b>';
}

function factorial($in){
   if($in<2) return 1;
   for($f=2; $in-1>1; $f*=$in--){}
   return $f;
}

?>
But that is not what I want. I want to return an actual date based on the possiblities. And obviously the date wouldn't be fixed because that's not how the lottery works. It would be different every time but based on the possibilities. And if there were 100 combinations and you played 100 time that doesn't mean your numbers would come up.
Post Reply