Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7057081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:56:36+00:00 2026-05-28T03:56:36+00:00

Possible Duplicate: Algorithm for generating a random number Hi i need to assign a

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:56:37+00:00Added an answer on May 28, 2026 at 3:56 am

    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 
    

    ?>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How exactly should I implement a shuffle or random-number algorithm for an
Possible Duplicate: Algorithm to find minimum number of weighings required to find defective ball
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: C#: Is using Random and OrderBy a good shuffle algorithm? I want
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? I need to
Possible Duplicate: random string generation - two generated one after another give same results
Possible Duplicate: Generating all Possible Combinations I'm trying to do a little algorithm in
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.