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! Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/ 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 Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175251 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. Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175275 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 Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175393 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... Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175429 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 Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175450 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); ?> ?> Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175459 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); ?> Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175485 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)); Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175520 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? Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175522 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. Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175524 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. Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175532 Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 that works fine mate Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175534 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. Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175536 Share on other sites More sharing options...
Daney11 Posted February 2, 2007 Share Posted February 2, 2007 Not outputting anything to the browser. Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175538 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. Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175546 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 Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175583 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 =\ Link to comment https://forums.phpfreaks.com/topic/36744-how-to-generate-random-numbers/#findComment-175705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.