jhl84 Posted February 2, 2007 Share Posted February 2, 2007 Hi guys, i have this number, 878777886XXXXXX. The Xs represent random numbers that I want to generate. I want to generate 1000 numbers that does not repeat using php. can anyone help? thanks! Quote Link to comment Share on other sites More sharing options...
chronister Posted February 2, 2007 Share Posted February 2, 2007 http://us2.php.net/manual/en/function.rand.php Random Function is what your looking for Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 Hey mate, I think this is what you are looking for.... <?php for ($i=1; $i <= 10; $i++) { $number_start = "878777886"; $number_rand = mt_rand(100000, 999999); // XXXXXX = 6 Digits $number = "$number_start$number_rand"; echo $number.'<br />'; } ?> Just change the 10 in the for loop for 1000 or what ever number you want. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 2, 2007 Share Posted February 2, 2007 Neither one of the the above solutions guarantee that the random numbers will not repeat. That can be done with the following code: <?php $ok_rand = array(); for ($i=0;$i<1000;$i++) { $test_rand = rand(1,999999); // get a random number while(in_array($test_rand,$ok_rand)) $test_rand = rand(100000,999999); // have we seen this number already? Yes -- generate another $ok_rand[$i] = sprintf('%06d',$test_rand); // store unique number as a six digit number with leading zeros } // // use the stored numbers // ?> Ken Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 actually ken, if i've understood your code correctly, it doesn't ensure that the numbers won't repet either. although the probability is very slim, it is still possible that it would generate the same number twice in a row. here is some code that will make sure there's not a repeat. <?php $input = 878777886 . rand(100000, 999999); $rand_array = array(); for($i = 0; $i < 1000; $i++){ foreach($rand_array as $key => $val){ (($input != $val) ? ($bool = false) : ($bool = true)); } (($bool == false) ? ($rand_array[] = $input) : ($i--)); } print_r($rand_array); ?> untested. i'm curious... will the value of $input constantly change, or will it stay the same? i'm not sure how that works... Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 Hi boo_lolly.. Not that i needed this i just fancied testing the 2 codes you 2 have posted and i tested yours with only 10 results wanting to be displayed and got... Notice: Undefined variable: bool in c:\www\test\milk.php on line 9 Fatal error: Maximum execution time of 30 seconds exceeded in c:\www\test\milk.php on line 7 Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 Hi boo_lolly.. Not that i needed this i just fancied testing the 2 codes you 2 have posted and i tested yours with only 10 results wanting to be displayed and got... Notice: Undefined variable: bool in c:\www\test\milk.php on line 9 Fatal error: Maximum execution time of 30 seconds exceeded in c:\www\test\milk.php on line 7 hmmmmm... maybe there's a better way to approach this... <?php <?php $rand_array = array(); for($i = 0; $i < 1000; $i++){ $input = 878777886 . rand(100000, 999999); foreach($rand_array as $key => $val){ (($input != $val) ? ($bool = false) : ($bool = true; exit;)); } (($bool == false) ? ($rand_array[] = $input) : ($i--)); } print_r($rand_array); ?> ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 or how about this.... <?php $rand_array = array(); for($i = 100000; $i < 999999; $i++){ $rand_array[] = $i; } shuffle($rand_array); $i = 0; $append_array = array(); foreach($rand_array as $key => $val){ $append_array[] = 878777886 . $val; (($i < 1000) ? (continue) : (exit)); $i++; } print_r($append_array); ?> Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 Nope boo_lolly, Both of them giving out parse error's on line 7 and 8. <?php $rand_array = array(); for($i = 100000; $i < 999999; $i++){ $rand_array[] = $i; } shuffle($rand_array); $i = 0; $append_array = array(); foreach($rand_array as $key => $val){ $append_array[] = 878777886 . $val; (($i < 1000) ? (continue) : (exit)); $i++; } print_r($append_array); ?> this one is giving out parse error on line 12 which is (($i < 1000) ? (continue) : (exit)); Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 i don't understand why the second one would be giving a parse error... what's the exact error? and for what code? Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 I just says Parse error: parse error in c:\test\milk.php on line 12 not sure how to use error reporting in scripts... might have to start using it lol. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 try doing this, and see if it gives an error: <?php $val = 10101; echo 84718947491 . $val; ?> if it brings an error, then that's the problem. appending an intiger to an existing integer. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 that works fine mate Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 ok meow: <?php $rand_array = array(); for($i = 100000; $i < 999999; $i++){ $rand_array[] = $i; } shuffle($rand_array); $i = 0; $append_array = array(); foreach($rand_array as $key => $val){ $append_array[] = 878777886 . $val; if($i < 1000){ continue; }else{ exit; } $i++; } ?> this is, in theory, no different than the previous code... but try it and see if that works. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 Not outputting anything to the browser. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 it's not supposed to. but the values are stored in the array, perfectly, i bet. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 2, 2007 Share Posted February 2, 2007 Just came back to the conversation. My original code works fine. The trick is the while statement within the for loop. It will loop until a random number is generated that is not in the array. To check, add these three lines to the end of my script: <?php $unq = array_unique($ok_rand); $dif = array_diff($ok_rand,$unq); if (empty($dif)) echo "All generated random numbers are unique"; ?> Ken Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 well, it looks like i didn't understand your code correctly =\ Quote Link to comment 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.