Monkuar Posted January 27, 2012 Share Posted January 27, 2012 function choose_numbers($number,$max) { global $ibforums,$std; srand( (double)microtime() * 1000000 ); $array = array(); $i = 0; while($i!=$number) { $array[] = mt_rand(1,$max); $i++; } $c = count($array); while($c != $number) { $array[] = mt_rand(1,$max); $c = count($array); if($c == $number) { $array = array_unique($array); $c = count($array); } } return $array; } $newnumbers = $this->choose_numbers(3,36); echo "{$newnumbers['0']}|{$newnumbers['1']}|{$newnumbers['2']}"; This echo's out my lottery balls 0|32|22 or technically RANDNuMbER|RANDNUMBER|RANDNUMBER up to 36 is the highest number. Now my issue is, I don't want 2 numbers of the same Value, EVER. Sometimes it does 2|3|2 which is BAD (for my lottery system) is there anyway I can make it so it never will echo out 2 (OR 3) of the same number? Quote Link to comment https://forums.phpfreaks.com/topic/255868-mt_rand-to-never-pick-2-values-of-equal-value/ Share on other sites More sharing options...
Monkuar Posted January 27, 2012 Author Share Posted January 27, 2012 srand( (double)microtime() * 1000000 ); $array = array(); $i = 0; while($i!=$number) { $array[] = mt_rand(1,$max); $i++; } return $array; is that function sorryi couldn't edit my post? Quote Link to comment https://forums.phpfreaks.com/topic/255868-mt_rand-to-never-pick-2-values-of-equal-value/#findComment-1311613 Share on other sites More sharing options...
AyKay47 Posted January 27, 2012 Share Posted January 27, 2012 1. if you are running any PHP version greater than 4.2.0, srand() is not needed. 2. don't use the global keyword, you don't even use those variables in your function. 3. I would check for the existence of the random value in the array before inserting, making sure that all values will be unique. $array = array(); $i = 0; while($i != $number) { $rand = mt_rand(1,$max); if(!in_array($rand,$array)) $array[] = $rand; $i++; } return $array; Quote Link to comment https://forums.phpfreaks.com/topic/255868-mt_rand-to-never-pick-2-values-of-equal-value/#findComment-1311651 Share on other sites More sharing options...
Monkuar Posted January 27, 2012 Author Share Posted January 27, 2012 1. if you are running any PHP version greater than 4.2.0, srand() is not needed. 2. don't use the global keyword, you don't even use those variables in your function. 3. I would check for the existence of the random value in the array before inserting, making sure that all values will be unique. $array = array(); $i = 0; while($i != $number) { $rand = mt_rand(1,$max); if(!in_array($rand,$array)) $array[] = $rand; $i++; } return $array; 1 Problem sometimes it displays 1 missing number as a blank? "17|29|" Quote Link to comment https://forums.phpfreaks.com/topic/255868-mt_rand-to-never-pick-2-values-of-equal-value/#findComment-1311762 Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2012 Share Posted January 27, 2012 Yes, that's because the counter is incremented irrespective of whether the generated random number was in the array or not. Write the if() conditional in standard format, with curly braces, and move the $i++ within the curly braces. That should take care of it. Quote Link to comment https://forums.phpfreaks.com/topic/255868-mt_rand-to-never-pick-2-values-of-equal-value/#findComment-1311765 Share on other sites More sharing options...
Monkuar Posted January 27, 2012 Author Share Posted January 27, 2012 Yes, that's because the counter is incremented irrespective of whether the generated random number was in the array or not. Write the if() conditional in standard format, with curly braces, and move the $i++ within the curly braces. That should take care of it. if(!in_array($rand,$array)) { $array[] = $rand; $i++; } Marked as Resolved. Kudos. Quote Link to comment https://forums.phpfreaks.com/topic/255868-mt_rand-to-never-pick-2-values-of-equal-value/#findComment-1311766 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.