Jump to content

How to Generate Random Numbers?


jhl84

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.