0

I have to generate 16 digit random number something like recharg card pin number.(including 0 at starting place) This is my code

function gen_num($len) {
    $rand   = '';
    while(!(isset($rand[$len-1]))){
        $rand   .= mt_rand(0,9);
    }
    return substr( $rand , 0 , $len );
}

It works fine but takes time to generate.if i want to generate 1000 pin at a same time like

foreach($i=0;$i<=1000;$i++){
 get_num(16)
}

It takes more time.What is the best way to generate this kind of pin?

Niroj Adhikary
  • 1,775
  • 18
  • 30

3 Answers3

4

you may try this function.this will work fine.

function getRandomNumber($len = "15")
{
    $better_token = $code=sprintf("%0".$len."d", mt_rand(1, str_pad("", $len,"9")));
    return $better_token;
}

for($i=0;$i<=1000;$i++){
    $codes[]=getRandomNumber(16);
}

in $code array you will get all 16 digit number with including 0.Output will be generated like below

Array
(
    [0] => 6432618102990092
    [1] => 5412363498471678
    [2] => 8415870470926167
    [3] => 2724858690053225
    [4] => 3719462379813195
    [5] => 0322053456788270
    [6] => 5085590323433281
    [7] => 3586620986461640
    [8] => 5041978429071606
    [9] => 0722051602788270
.....
Shrey Prajapati
  • 559
  • 2
  • 8
1
$a = '';
      for ($i = 0; $i<16; $i++) 
    {
        $a .= mt_rand(0,9);
    }

you can use this function for generate random number.

it's possible to same number generate twise so you can put this number in array and check in array number is already there or not.

    function gen_num() {
        $rand   = 0;
        for ($i = 0; $i<15; $i++) 
            {
                $rand .= mt_rand(0,9);
            }
        return $rand;
    }



foreach($i=0;$i<=1000;$i++){
 echo get_num();
}

o/p

1992282192849338
2539119565182977
8186185479424499
9762937534145386
5565837949946845
6487684896937977
1437518184397774
1586381543857153
5673128433587996
4988724673517522
8323477255626612
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
  • 1
    `generate 16 digit random number` - your method would not provide a 16 digit number, as `1` could be return from your `rand()`, which is 15 digits too short. – Sean Dec 01 '15 at 05:34
  • now your answer will always start with `0`. Do you have something wrong with `1-9` in the 1st position? – Sean Dec 01 '15 at 05:39
  • no it's random generate you can use rand(1,9) instead of mt_rand(0,9) – Parth Chavda Dec 01 '15 at 05:42
  • but you are starting with `$a = 0;`/`$rand = 0;` and then concatenating to them `$a .=`/`$rand .=` so your 16 digit numbers will always start with `0` using this code, instead of random `0-9` at the start. – Sean Dec 01 '15 at 05:49
  • i have updated my ans please check now no 0 come to first – Parth Chavda Dec 01 '15 at 05:50
  • ya i got it @Sean i have change my ans – Parth Chavda Dec 01 '15 at 05:50
0

Something like this would do just fine:

function random_digits($length) {
    $length = intval($length, 10);
    $output = '';
    for ($i = 0; $i < $length; $i++) {
        $output .= mt_rand(0, 9);
    }
    return $output;
}
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52