Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a unique alphanumeric 10-character string

Tags:

random

unique

I'm looking to create a simple short-lived reservation system, and I'd like to generate confirmation numbers that are

  • unique
  • random-looking
  • alphanumeric
  • short-ish, at least much shorter than 32 character-long strings returned by sha1

I'm only looking to have ~500 reservations, so I don't imagine high likelyhood of collissions.

One idea I had is generate an sha1 hash based on a date-time stamp and username, then truncating it to its first 10 characters. Would something like that be reliably unique enough for the purposes of processing ~500 reservations?

like image 302
saturdayplace Avatar asked Jan 21 '09 23:01

saturdayplace


People also ask

How do you generate random unique alphanumeric strings?

There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.

How do you make a unique string?

If you simply want to generate a unique string and it does not have to be cryptographically secure, then consider using the uniqid() function. It returns a unique identifier based on the current timestamp.

What is alphanumeric string example?

Alphanumeric is a description of data that is both letters and numbers. For example, "1a2b3c" is a short string of alphanumeric characters. Alphanumeric is commonly used to help explain the availability of text that can be entered or used in a field, such as an alphanumeric password field.

What is string in alphanumeric?

An alphanumeric string is a string that contains only alphabets from a-z, A-Z and some numbers from 0-9. Examples: Input: str = “GeeksforGeeks123”


1 Answers

There should be no difference in the randomness of any given bit of a SHA-1 hash, so that's possible. Another way would be to fold the hash into itself using XOR until you have 60 bits worth of data, then encode it using Base 64 to get a mostly alpha-numeric result.

This is only necessary if you want to be able to generate the same Id repeatedly for the same input data. Otherwise, if a random id that you generate once, and hold onto after that, use Anders' suggestion. If you get a conflict, just generate another one.

like image 131
Eclipse Avatar answered Nov 14 '22 02:11

Eclipse