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 8565483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:26:13+00:00 2026-06-11T17:26:13+00:00

I have this small internal project that inserts occasional entries into a MySQL database.

  • 0

I have this small internal project that inserts occasional entries into a MySQL database. I have a column named “idChar” were I set it’s value to a randomly generated string using 62 possible characters with a length of 31.

Today I discovered that a new entry just so happened to have the same exact idChar as an entry from several months ago. I am now checking for duplicate entries before saving them, but this made me think about the odds of this happening and I am curious to know if my implementation of generating these random keys is flawed. Getting a duplicate should be roughly 1 in 62^31 right?

function getCode($len)
{
    //$len = 10;
    $base='ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstwxyz123456789';
    $max=strlen($base)-1;
    $linkCode='';
    mt_srand((double)microtime()*1000000);
    while (strlen($linkCode)<$len+1)
      $linkCode.=$base{mt_rand(0,$max)};

    return $linkCode;
}

$idChar=getCode(30);

//code to insert into MySQL here
  • 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-06-11T17:26:15+00:00Added an answer on June 11, 2026 at 5:26 pm

    The odds of getting a duplicate would be calculated as per the birthday problem, because that’s how you calculate the chance of collisions for the output of a function that produces a randomly chosen output from a discrete codomain. In effect you want to calculate the chance that among a pool of selections made randomly any two selections are the same.

    You should also completely drop the mt_srand call as it is not necessary, and it’s likely to provide worse seeds than what PHP will do automatically. Consider that the output of microtime (in my system at least) is like

    0.29574400 1348356024
    

    which means that you only have 1 million different seeds available as the last two digits of the float are always zeroes and the (double)microtime() cast completely ignores the seconds part (it would be a lousy seed anyway).

    Assuming that the random number generator produces the same sequence of random numbers whenever it is seeded with the same seed then in effect you only have 1 million possible random codes instead of 62^31 — quite a decrease! Fortunately it is documented that this does not happen on PHP 5.2.1 onwards.

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

Sidebar

Related Questions

I have this small class called City that simply holds some information about a
I have this small PHP script that takes a POST variable and writes it
I have this small piece of code that basically takes a list and runs
Consider this scenario. I have a small enterprise project dev environment. We use archiva
I have a small project that I want to deploy to both Heroku and
We have an application that communicates with printers using their internal printer fonts. This
I just inherited a project for a small company. This is a completely internal
I have this small class looking like this: private static int field1 = -
I have this small snippet to change a bit of html when a user
In my c# application i have this small form which is used to set

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.