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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:17:54+00:00 2026-05-23T09:17:54+00:00

Will this following function always generate a unique string? What will be the length

  • 0

Will this following function always generate a unique string? What will be the length range of generated string from the following function? Can it be improved to generate more uniqueness?

base_convert(mt_rand(10, 99) . intval(microtime(true) * 100), 8, 36);

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-23T09:17:55+00:00Added an answer on May 23, 2026 at 9:17 am

    Keep in mind that it is generally a bad idea to write your own random/unique functions. Like KingCrunch mentioned PHP had a built-in function uniqid that does precisely what you want.

    To answer your question:

    The function will not always generate a unique string, it is impossible to do so. However good functions make it extremely unlikely (magnitudes of times smaller than winning the lottery) and thus unique enough in practice.

    length:

    • mt_rand(10,99) : generates a number
      in the range [10,99] (2 digits)
    • intval(microtime(true)*100) will get
      the current unix timestamp plus 2
      digits of the milliseconds. The timestamp is (at the moment) 10 digits long (and will remain so for about a century) so the total number of digits is 12 (decimal) digits

    It then treats the number as a base_8 number this will fail often, as base_8 does not know the number 8 or 9. I do not know the exact PHP behaviour in this case but it is invalid nonetheless. If you act like the number is treated as base 10, you have the following scenario question: how much digits (base 36) are needed to represent numbers between 10^13 (if mt_rand generates 10) and 10^14 (if mt_rand generates 99).

    The smallest number where base_36 needs 9 digits for is 2,821,109,907,457 which is lower than the underbound of your number. The largest number it can represent with 9 digits is however 101,559,956,668,416 (~10^14). Thus it will generate a 9-digit base_36 number.

    [edit]
    I saw you specifically needed a 6-character unique string. Keep in mind that 6-characters is relatively short so uniqueness is hard to guarantee. Your best bet would still be

    substr(uniqid(), 0, 6);
    

    It will be better than any function you manage to come up on your own with.[/edit]

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

Sidebar

Related Questions

I have the following function, it will always return True. Any ideas why and
I would like to do the following but I don't think this will work:
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following criteria query: var x = SomeCriteria.AddOrder(new Order(Name, true)).List(); This will order
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
Is there a reason why this query doesn't work? The following query will work
Consider the following Javascript function (1): function setData(domElement) { domElement.myDataProperty = { 'suppose': 'this',
I wrote the following class in javascript function main() { this.area ; this.$= function(target)
Will this code ever wait on the mutex inside the producer's void push(data) ?
Will this validate in XHTML? <span>hello<span>world</span></span>

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.