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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:14:46+00:00 2026-05-19T13:14:46+00:00

I need to generate string that meets the following requirements: it should be a

  • 0

I need to generate string that meets the following requirements:

  1. it should be a unique string;
  2. string length should be 8 characters;
  3. it should contain 2 digits;
  4. all symbols (non-digital characters) should be upper case.

I will store them in a data base after generation (they will be assigned to other entities).

My intention is to do something like this:

  1. Generate 2 random values from 0 to 9—they will be used for digits in the string;
  2. generate 6 random values from 0 to 25 and add them to 64—they will be used as 6 symbols;
  3. concatenate everything into one string;
  4. check if the string already exists in the data base; if not—repeat.

My concern with regard to that algorithm is that it doesn’t guarantee a result in finite time (if there are already A LOT of values in the data base).

Question: could you please give advice on how to improve this algorithm to be more deterministic?

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-19T13:14:47+00:00Added an answer on May 19, 2026 at 1:14 pm
    1. it should be unique string;
    2. string length should be 8 characters;
    3. it should contains 2 digits;
    4. all symbols (non-digital characters) – should be upper case.

    Assuming:

    • requirements #2 and #3 are exact (exactly 8 chars, exactly 2 digits) and not a minimum
    • the “symbols” in requirement #4 are the 26 capital letters A through Z
    • you would like an evenly-distributed random string

    Then your proposed method has two issues. One is that the letters A – Z are ASCII 65 – 90, not 64 – 89. The other is that it doesn’t distribute the numbers evenly within the possible string space. That can be remedied by doing the following:

    1. Generate two different integers between 0 and 7, and sort them.
    2. Generate 2 random numbers from 0 to 9.
    3. Generate 6 random letters from A to Z.
    4. Use the two different integers in step #1 as positions, and put the 2 numbers in those positions.
    5. Put the 6 random letters in the remaining positions.

    There are 28 possibilities for the two different integers ((8*8 – 8 duplicates) / 2 orderings), 266 possibilities for the letters, and 100 possibilities for the numbers, the total # of valid combinations being Ncomb = 864964172800 = 8.64 x 1011.


    edit: If you want to avoid the database for storage, but still guarantee both uniqueness of strings and have them be cryptographically secure, your best bet is a cryptographically random bijection from a counter between 0 and Nmax <= Ncomb to a subset of the space of possible output strings. (Bijection meaning there is a one-to-one correspondence between the output string and the input counter.)

    This is possible with Feistel networks, which are commonly used in hash functions and symmetric cryptography (including AES). You’d probably want to choose Nmax = 239 which is the largest power of 2 <= Ncomb, and use a 39-bit Feistel network, using a constant key you keep secret. You then plug in your counter to the Feistel network, and out comes another 39-bit number X, which you then transform into the corresponding string as follows:

    1. Repeat the following step 6 times:
    2. Take X mod 26, generate a capital letter, and set X = X / 26.
    3. Take X mod 100 to generate your two digits, and set X = X / 100.
    4. X will now be between 0 and 17 inclusive (239 / 266 / 100 = 17.796…). Map this number to two unique digit positions (probably easiest using a lookup table, since we’re only talking 28 possibilities. If you had more, use Floyd’s algorithm for generating a unique permutation, and use the variable-base technique of mod + integer divide instead of generating a random number).
    5. Follow the random approach above, but use the numbers generated by this algorithm instead.

    Alternatively, use 40-bit numbers, and if the output of your Feistel network is > Ncomb, then increment the counter and try again. This covers the entire string space at the cost of rejecting invalid numbers and having to re-execute the algorithm. (But you don’t need a database to do this.)

    But this isn’t something to get into unless you know what you’re doing.

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

Sidebar

Related Questions

I need to generate a unique string that is 30 characters in length. At
I need to generate some passwords, I want to avoid characters that can be
EDIT: I need to generate a string of 7 chars that is based on
I need to generate the following XML: <Learner xmlns=some.domain.api> <ActivationDate>1999-05-31T11:20:00</ActivationDate> <EmailAddress>String content</EmailAddress> <ExpirationDate>1999-05-31T11:20:00</ExpirationDate> <FederalId>String
I need to generate thumbnails from a set of jpg's that need to have
I need to generate a new interface at run-time with all the same members
I need to generate random tokens so that when I see them later I
I am looking for a good DSL Tool / Framework that meets the following
I need help converting a string that contains a number in scientific notation to
I need to generate a unique hash code for an object, based on its

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.