Jump to content

Create random IDs


Ryahn

Recommended Posts

I have a mailer script that will be mailing grabbing info from a database. In that database there is an ID field, but I have it right now only doing AutoNumber but I want to be able to make random 6 digit ID numbers to that way when a form is submitted, it will send that user an ID and their password. I dont know how to do this, so this is why I am asking.

Link to comment
https://forums.phpfreaks.com/topic/204683-create-random-ids/
Share on other sites

Thanks for that. I ended just looking around and found this.

 

function generate_numbers($min, $max, $anz)
{
     $array = range($min, $max);
     srand ((double)microtime()*1000000);
     for($x = 0; $x < $anz; $x++)
     {
          $i = rand(1, count($array))-1;
          $erg[] = $array[$i];
          array_splice($array, $i, 1);
     }
     return $erg;
}

// 5 eindeutige Zahlen im Bereich von 1 bis 100 ermitteln
$zufalls_array = generate_numbers(1, 100, 5);
echo join("; ", $zufalls_array);

 

which I then did a little tweak

 

function generate_numbers($min, $max, $anz)
{
     $array = range($min, $max);
     srand ((double)microtime()*10000);
     for($x = 0; $x < $anz; $x++)
     {
          $i = rand(1, count($array))-1;
          $erg[] = $array[$i];
          array_splice($array, $i, 1);
     }
     return $erg;
}

// 5 eindeutige Zahlen im Bereich von 1 bis 100 ermitteln
$zufalls_array = generate_numbers(1, 100, 3);
$code = join("", $zufalls_array);

 

Now the $code just gets inserted into the ID field of the database and thats it.

 

 

Thanks for it.

Link to comment
https://forums.phpfreaks.com/topic/204683-create-random-ids/#findComment-1071647
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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