what's the Best way to generate 16 length random number in php? -


i want generate random number code in php without repeat , 16 lengths. what's best way this? use code :

$possible = '0123456789'; $code = ''; $i = 0; while ($i < 14) {     $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);     $i++; } echo($code); 

but generating 1 random number. want 30000 random numbers. shall ?

i use code not generate 16 length :

<?php      $con=mysqli_connect("localhost","root","","test1");     // check connection     if (mysqli_connect_errno()) {         echo "failed connect mysql: " . mysql_connect_error();     }     ($s=0;$s<10;$s++) {         $possible = '0123456789';         $code = '';         $i = 0;     while ($i < 16) {         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);         $i++;     }     echo($code);     echo nl2br("$code <br/>");      mysqli_query($con,"insert test (id, code, type, used)     values ('', '".$code."','1', '0')");     }     ($s=0;$s<10;$s++) {         $possible = '0123456789';         $code = '';         $i = 0;     while ($i < 16) {         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);         $i++;     }     echo($code);     echo nl2br("$code <br/>");      mysqli_query($con,"insert test (id, code, type, used)     values ('', '".$code."','2', '0')");     }     ($s=0;$s<10;$s++) {         $possible = '0123456789';         $code = '';         $i = 0;     while ($i < 16) {         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);         $i++;     }     echo($code);     echo nl2br("$code <br/>");      mysqli_query($con,"insert test (id, code, type, used)     values ('', '".$code."','3', '0')");     }     mysqli_close($con); ?> 

snippet no repeating digits:

$len=14; $last=-1; ($i=0;$i<$len;$i++) {         {         $next_digit=mt_rand(0,9);     }     while ($next_digit == $last);     $last=$next_digit;     $code.=$next_digit; } 

snippet no duplicates within 30000 (but repeating digits allowed):

$codes=array(); while (count($codes) < 30000) {     $code=rand(pow(10,13),pow(10,14)-1);     $codes["$code"]=1; } 

the codes stored keys of array (just save memory).

there might better solutions problem (especially more efficient ones) - 1 short ;-) - due integration of kolink's solution.

hope helps bit.

*jost


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -