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

  • Home
  • SEARCH
  • 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 953389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:02:39+00:00 2026-05-16T00:02:39+00:00

I have a randomly generated link id which is 8 digits long with 50

  • 0

I have a randomly generated link id which is 8 digits long with 50 values per digit, which means 4×10^13 possible combinations (i think). I have about ten-thousands of queries per day.

My question is, should I check 4 tables each query for duplicates, or skip it? or make it 10 digits so that it definitely won’t be a match?

edit:

my (probably copied) generator

// START Generates Random String
function genRandString($len=8){
$base='ABCDEFGHKLMNPQRSTWXYZabcdefghjkmnpqrstwxyz23456789';
$max=strlen($base)-1;
$randstring1 ='';
mt_srand((double)microtime()*1000000);
while (strlen($randstring1)<$len+1)
$randstring1.=$base{mt_rand(0,$max)};
return $randstring1;
}
// END Generates Random String
  • 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-16T00:02:40+00:00Added an answer on May 16, 2026 at 12:02 am

    It depends on the quality of the pseudorandom number generator. You might have insufficient entropy so you’re more likely to get a collision that you realize.

    Is there a reason you aren’t using UUID()? It seems like the best solution that is designed for this purpose.

    Anyway, I don’t recommend checking for duplicates before you insert. This is subject to race conditions, that is, someone could insert the duplicate value in the moment after you check, but before you insert. So you’d have to handle duplicate key violation exceptions anyway. Better to just try the insert (without checking first) and handle exceptions as needed.


    Re your comments and your algorithm: I would not use that hashing scheme. You have fewer than 24 bits of information in four digits of 50 distinct values. So your chance of collision is pretty significant once you have a few thousand rows in your database.

    How about this solution: Use a monotonically increasing primary key value, e.g AUTO_INCREMENT. To change this number into an alphanumeric string, use base_convert():

    $id = 12345678;
    $str = base_convert($id, 10, 36);
    echo "$str\n";
    

    The result is 7clzi.

    If you are worried about confusion in letters like 1, l, i, 0, o, you can do some custom substitutions:

    $from = array('1', 'l', 'i', '0', 'o');
    $to   = array('A', 'B', 'C', 'D', 'E');
    $str = str_replace($from, $to, $str);
    

    Now the value 12345678 is converted to 7cBzC. When someone requests a page by this code, do the conversion in reverse:

    $code = str_replace($to, $from, $code);
    $id = base_convert($code, 36, 10); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a randomly generated string s=t&^%JHGgfdteam*&HGEdfg , what is the best approach
While running a batch file in Windows XP I have found randomly occurring error
Since upgrading to 2008 I and many people here have noticed that randomly VS
I have a 2D image randomly and sparsely scattered with pixels. given a point
I have 1 red polygon say and 50 randomly placed blue polygons - they
I have a list of objects and I want to reorder them randomly on
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I'm trying to create an android application which will generate random series of values
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could

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.