3

I want to generate a random number code in PHP without repeat and with 16 lengths. What's the best way to do this? I use this code :

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

But that is generating 1 random number. I want 30000 random numbers. What shall I do ?

i use this code too but that is not generate 16 length :

<?php

    $con=mysqli_connect("localhost","root","","test1");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysql_connect_error();
    }
    for ($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 INTO test (ID, Code, Type, Used)
    VALUES ('', '".$code."','1', '0')");
    }
    for ($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 INTO test (ID, Code, Type, Used)
    VALUES ('', '".$code."','2', '0')");
    }
    for ($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 INTO test (ID, Code, Type, Used)
    VALUES ('', '".$code."','3', '0')");
    }
    mysqli_close($con);
?>
mr.sohrab
  • 51
  • 2
  • 9
  • You say 16 in your title, and 14 in your question... which is it? – Niet the Dark Absol Jul 18 '13 at 09:25
  • possible duplicate of [Secure random number generation in PHP](http://stackoverflow.com/questions/1182584/secure-random-number-generation-in-php) – BenLanc Jul 18 '13 at 09:26
  • That code works just fine, it returned `64194616012765`. What do you mean by 30000 random number? – Mr.Web Jul 18 '13 at 09:27
  • Do you mean that you don't want to have repeating digits or you don't want to have two equal numbers within the 30000? – Jost Jul 18 '13 at 09:35
  • i need 3000 random number . but this code give me one code and non of code duplicated. all of the code must be Uniqe – mr.sohrab Jul 18 '13 at 09:37
  • This question already answered many times on stackoverflow http://stackoverflow.com/questions/13169025/generate-a-random-number-with-pre-defined-length-php?rq=1 – Afroz Alam Jul 18 '13 at 10:27
  • Use uniqid with more entropy, and substr to chop off. – Prasanth Jul 18 '13 at 10:35
  • 2
    So you basically want someone to modify the code you posted because you can't get it to do 3000 iterations? – N.B. Jul 18 '13 at 10:35
  • random != uniqe, but I agree a `for($i=0;$i<3000;$i++){` would be a start.... – Wrikken Jul 18 '13 at 18:09

2 Answers2

4

A random 14-digit number may be generated simply by using rand(pow(10,13),pow(10,14)-1) - PHP can handle integer values up to 251-1, since it silently converts them to double-precision floats if they get too big.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

Snippet for no repeating digits:

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

Snippet for no duplicates within 30000 (but repeating digits are allowed):

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

The codes are stored as the keys of the array (just to save some memory).

There might be better solutions for this problem (especially with more efficient ones) - but this one is really short ;-) - due to the integration of Kolink's solution.

Hope it helps a bit.

*Jost

Jost
  • 1,549
  • 12
  • 18