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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:12:22+00:00 2026-06-09T22:12:22+00:00

I want to generate a 9 digit unique random string. Currently I am using

  • 0

I want to generate a 9 digit unique random string. Currently I am using

Guid.NewGuid().ToString().Replace("-","").Substring(0,9)

But I am afraid it would have a collision very soon. Is there any better way for this or would this be OK?

  • 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-09T22:12:23+00:00Added an answer on June 9, 2026 at 10:12 pm

    If you take a sub string of a GUID you are not guaranteed randomness uniqueness at all.

    See my answer to a older SO question to fulfill your randomness requirement. Here is the basic code to do it.

    public static string CreateRandomString(int length)
    {
        length -= 12; //12 digits are the counter
        if (length <= 0)
            throw new ArgumentOutOfRangeException("length");
        long count = System.Threading.Interlocked.Increment(ref counter);
        Byte[] randomBytes = new Byte[length * 3 / 4];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(randomBytes);
    
        byte[] buf = new byte[8];
        buf[0] = (byte)count;
        buf[1] = (byte)(count >> 8);
        buf[2] = (byte)(count >> 16);
        buf[3] = (byte)(count >> 24);
        buf[4] = (byte)(count >> 32);
        buf[5] = (byte)(count >> 40);
        buf[6] = (byte)(count >> 48);
        buf[7] = (byte)(count >> 56);
        return Convert.ToBase64String(buf) + Convert.ToBase64String(randomBytes);
    }
    

    it gives you 12 digits of counting to prevent collisions and any additional digits you want of randomness. you can modify the code as you want to make shorter than 12 digit strings.

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

Sidebar

Related Questions

I want to generate atleast 1000 unique random numbers with 10 digit using javascript
I want to generate a 6 digit random number using the PHP mt_rand() function.
I want to generate random integers between 0-9 (inclusive on both ends), but I
I want to generate 1M random (appearing) unique alphanumeric keys and store them in
I want to auto-generate a unique 8-10 character ID string that includes a checksum
In my application, i want to generate 9 digit random numbers such that they
I need to generate a nine digit numeric code (random preferably) which is unique
I'm using C# and I need to generate a random 10 digit number. So
i have made a code to generate 11 digit random number and i want
I want to generate a random timestamp and add a random increment to it

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.