Jump to content

Lines of numbers that don't duplicate


Mutley

Recommended Posts

Hard to explain but here goes.

 

I need a script that creates 64 lines of 6 numbers (that range from 1 to 49). In each line, there can't be any more of any one number. E.g. it shouldn't do: 24/30/7/22/40/7  because 7 displays more than once.

 

I've already done the code for this:

 

<?php
$c=1;
while($c < 64){
	$n1=rand(1,49);// NUMBER 1

	$n2=rand(1,49);// NUMBER 2
		while(($n2 == $n1) || ($n2 == $n3) || ($n2 == $n4) || ($n2 == $n5) || ($n2 == $n6)){
			$n2=rand(1,49);}
	$n3=rand(1,49);// NUMBER 3
		while(($n3 == $n1) || ($n3 == $n2) || ($n3 == $n4) || ($n3 == $n5) || ($n3 == $n6)){
			$n3=rand(1,49);}
	$n4=rand(1,49);// NUMBER 4
		while(($n4 == $n1) || ($n4 == $n2) || ($n4 == $n3) || ($n4 == $n5) || ($n4 == $n6)){
			$n4=rand(1,49);}
	$n5=rand(1,49);// NUMBER 5
		while(($n5 == $n1) || ($n5 == $n2) || ($n5 == $n3) || ($n5 == $n4) || ($n5 == $n6)){
			$n5=rand(1,49);}
	$n6=rand(1,49);// NUMBER 6
		while(($n6 == $n1) || ($n6 == $n2) || ($n6 == $n3) || ($n6 == $n4) || ($n6 == $n5)){
			$n6=rand(1,49);}

	echo "<tr>
			<td width='40'>$c</td>
			<td align='center'>$n1</td>
			<td align='center'>$n2</td>
			<td align='center'>$n3</td>
			<td align='center'>$n4</td>
			<td align='center'>$n5</td>
			<td align='center'>$n6</td>
		</tr>";
	$c++;
}
?>

 

 

But the next part I need to do, is no line of the 64 generated can have the same combination of 3 numbers.

 

E.g.

LineA = 34/28/16/4/46/1

LineB = 11/32/47/21/1/4

LineC = 28/4/46/11/44/10

 

LineC has 3 numbers the same as LineA (28, 4 and 46) this isn't allowed...

 

But I have no idea how to do this?

 

Any ideas?

 

Thanks in advance,

Nick.

 

Link to comment
Share on other sites

You got it. ;) It's to find the best numbers if you were to purchase 64 lines.

 

I get the following error with your array:

Warning: array_pop() [function.array-pop]: The argument should be an array in /home/graphics/public_html/lotto.php on line 6

 

Thanks a lot for your help!

Link to comment
Share on other sites

function luckyDip() {
$validNumbers = range(1,49);
shuffle($validNumbers);
$myNumbers = array();
for ($i = 0; $i < 6; $i++) {
	$myNumbers[] = array_pop($validNumbers);
}
return $myNumbers;
}

$validSets = array();

$i = 0;
while ($i < 64) {
$luckyDip = luckyDip();
$matched3 = False;
foreach ($validSets as $validSet) {
	//	Rule of 3 test
	$matchCount = array_intersect($validSet,$luckyDip);
	if (count($matchCount) >= 3) {
		$matched3 = True;
		break;
	}
}
if (!$matched3) {
	$validSets[] = $luckyDip;
	$i++;
}
}

foreach ($validSets as $validSet) {
sort($validSet,SORT_NUMERIC);
$myNumberString = implode('/',$validSet);
echo $myNumberString.'<br />';
}

 

You know that for the UK lottery, if you break the possible numbers into groups:

1-9

10-19

20-29

30-39

40-49

There's a very high likelihood each draw that 3 balls will be from one of those groups, and 2 will be from a second group

Link to comment
Share on other sites

<table>
<tr>
<td>Row</td>
<td>N1</td>
<td>N2</td>
<td>N3</td>
<td>N4</td>
<td>N5</td>
<td>N6</td>
</tr>
<?php
   $c=1;
   while($c < 64){
      $n1=rand(1,49);// NUMBER 1
      $catch[$c][$n1]=TRUE;
      for($i=2;$i<=6;$i++)
      {
        $var='n'.$i;
        $$var=rand(1,49);// NUMBER 2
         while(isset($catch[$c][$$var])){$$var=rand(1,49);}
         $catch[$c][$$var]=TRUE;
      }
      
   
      echo "<tr>
            <td width='40'>$c</td>
            <td align='center'>$n1</td>
            <td align='center'>$n2</td>
            <td align='center'>$n3</td>
            <td align='center'>$n4</td>
            <td align='center'>$n5</td>
            <td align='center'>$n6</td>
         </tr>";
      $c++;
   }
?>

 

mayb this helps

Link to comment
Share on other sites

  • 3 weeks later...

That's brilliant,

 

I'm just wondering if it's possible to "influence" the random numbers by the occurance probability of the numbers in this CSV file?

 

I can parse the file but then I don't know how to make the PHP random function be influenced by a probability %? Is it possible?

 

http://www.national-lottery.co.uk/player/files/Lotto.csv

 

Cheers,

Nick.

Link to comment
Share on other sites

I'm just wondering if it's possible to "influence" the random numbers by the occurance probability of the numbers in this CSV file?
That's pretty easy.

When setting up the original list of numbers (1..49), you're creating an array of possible numbers using range(1,49).

What you can do is repeat certain numbers within that list:

$validNumbers = array(1,1,1,2,2,3,3,3,3,3,4,4,5,5,6,7,8,9,9,10,10....49);

Which increases your probability of getting a 1.

Then you need to ensure that you don't get duplicates within any given lucky dip.

function luckyDip() {
$validNumbers = range(1,49);
shuffle($validNumbers);
$myNumbers = array();
$drawn = 0;
do {
	$draw = array_pop($validNumbers);
	if (!in_array($draw,$myNumbers)) {
		$myNumbers[] = $draw;
		$drawn++;
	}
} while ($drawn < 6);
return $myNumbers;
}

If you want true non-linear distributions, then that's a deal more complex; though I do have a library for generating random numbers that follow a range of distribution curves such as gaussian and poisson.

Link to comment
Share on other sites

So the code would look like this, is this?

 

$validNumbers = array(1,1,1,2,2,3,3,3,3,3,4,4,5,5,6,7,8,9,9,10,10....49);

function luckyDip() {
$validNumbers = range(1,49);
...
...

 

or this

 

function luckyDip() {
$validNumbers = array(1,1,1,2,2,3,3,3,3,3,4,4,5,5,6,7,8,9,9,10,10....49);
...
...

 

And what happens if there were say, 1200+ numbers in that array, would it cause any issues with influencing it?

 

Many thanks,

Nick.

Link to comment
Share on other sites

My bad: like this.

function luckyDip() {
$validNumbers = array(1,1,1,2,2,3,3,3,3,3,4,4,5,5,6,7,8,9,9,10,10....49);
...
...

 

And what happens if there were say, 1200+ numbers in that array, would it cause any issues with influencing it?

Fractionally slower to generate a lucky dip, but not so as you'd notice, and the distribution generated by the shuffle would be as random as any random selection in PHP.

Link to comment
Share on other sites

Do the numbers have to be in order in the array like that?

 

I've done a test, and apparently #9 is most frequent and #21 is least frequent, yet they show up just as many times in the list? Is there a reason for this? I'm thinking 100 lines may be too small but there must be a way to influence it more.

Link to comment
Share on other sites

Do the numbers have to be in order in the array like that?

There's no need for them to be in order when you define the $validNumbers array, simly to have as many entries of each number as you need to reflect the higher probability of that number being drawn.

The order gets randomised anyway by shuffle($validNumbers);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.