Mutley Posted January 14, 2009 Share Posted January 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/ Share on other sites More sharing options...
Mark Baker Posted January 14, 2009 Share Posted January 14, 2009 $validNumbers = shuffle(range(1,49)); $myNumbers = array(); for ($i = 0; $i < 6; $i++) { $myNumbers[] = array_pop($validNumbers); } $myNumberString = implode('/',$myNumbers); Might help as a basic block of code This looks like a lottery game Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-736974 Share on other sites More sharing options...
Mutley Posted January 14, 2009 Author Share Posted January 14, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-736981 Share on other sites More sharing options...
Mark Baker Posted January 14, 2009 Share Posted January 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-736984 Share on other sites More sharing options...
Mutley Posted January 14, 2009 Author Share Posted January 14, 2009 Thanks a lot it's great! Anyway to order the lines somehow? Just to make it easier to check them! Cheers, Nick. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-736997 Share on other sites More sharing options...
dawsba Posted January 14, 2009 Share Posted January 14, 2009 <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 Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-736998 Share on other sites More sharing options...
Mark Baker Posted January 14, 2009 Share Posted January 14, 2009 foreach ($validSets as $key => $validSet) { sort($validSet,SORT_NUMERIC); $validSets[$key] = $validSet; } sort($validSets); foreach ($validSets as $validSet) { $myNumberString = implode('/',$validSet); echo $myNumberString.'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-737005 Share on other sites More sharing options...
Mutley Posted February 5, 2009 Author Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-755244 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-755349 Share on other sites More sharing options...
Mutley Posted February 5, 2009 Author Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-755354 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-755358 Share on other sites More sharing options...
Mutley Posted February 5, 2009 Author Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-755376 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/140803-lines-of-numbers-that-dont-duplicate/#findComment-755389 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.