How generate UNIQUE random number in php [duplicat

2019-06-04 04:55发布

问题:

Possible Duplicate:
Algorithm for generating a random number

Hi i need to assign a randomly generated number to some entries into the database and it must be unique. I use:

$random_number = mt_rand();
mysqli_query($db_connection, "INSERT INTO my_table (rand_num, blabla) VALUES ('$random_number', '$blabla')");

But ofc there could always be a slightly low chance that 2 random numbers will be the same. Then i could do something like:

function random_check($random_number) {
    require_once('db_access.php');
    $random_check = mysqli_query($db_connection, "SELECT rand_num FROM my_table");
    mysqli_close($db_connection);
    $result = 1;
    while($row = mysqli_fetch_array($random_check)){
            if ($row['rand_num'] == $random_number) {
             $result=0
           }
    }
        return $result;
};

$random_number = mt_rand();
$random_check = random_check($random_number);
if ($random_check == 0) {
      $random_number = mt_rand();
}

But this would only check the number once and there will still be a chance that the new generated number already exists into the db.

Any suggestions? Thanks

回答1:

Here is a method which you can use:

<?php
$num= mt_rand();
$con = mysql_connect("localhost","uname","password");
mysql_select_db("dbname",$con);
$sel_query  = "SELECT *  FROM  my_table WHERE rand_num =%d"; // query to select value 
$ins_query = "INSERT INTO my_table(rand_num) VALUES(%d)";    // query to insert value
$result =  mysql_query(sprintf($sel_query,$num),$con);
while( mysql_num_rows($result) != 0 ) {                      // loops till an unique value is found 
    $num = mt_rand();
    $result = mysql_query(sprintf($sel_query,$num),$con);
}
mysql_query(sprintf($ins_query,$num),$con); // inserts value 

?>



回答2:

Doing a SELECT rand_num FROM my_table and checking the resulting values is very, very inefficient. Use a WHERE clause:

do {
    $random_number = mt_rand();
    $query_object = mysqli_query($db_connection, "SELECT 1 FROM my_table WHERE rand_num = $random_number");
    $query_record = mysqli_fetch_array($query_object);
    if(! $query_record) {
        break;
    }
} while(1);

It is possible to write this code in lesser lines.



回答3:

function random_check($random_number) {
    require_once('db_access.php');
    $random_check = mysqli_query($db_connection, "SELECT rand_num FROM my_table");
    mysqli_close($db_connection);
    while($row = mysqli_fetch_array($random_check)){
            if ($row['rand_num'] == $random_number) {
             return false;
           }
    }
    return true;
};

while(!random_check(mt_rand()){
    $myNumbber = random_check(mt_rand();
}

--> Now u got unique number in "myNumber"



回答4:

md5( strtotime("now").$_SERVER['REMOTE_ADDR'] );

strtotime("now") would give you a unique number, but ifyou are using this script to process forms/uploads etc from multiple users, the ip address will just make certain of that (on the off chance that at the exact same second two entries are made... can that even happen?? hmm...)

md5() cos i felt like it :D