• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1381

# Random Unique ID generate

i want to generate 64 bit random integer for my website user ID, can you tell me what would be better algorithm so that i feel every ID would be unique but random.
0
marchent
3 Solutions

Commented:
How about a DES of a sequentially allocated number?
0

Author Commented:
DES is fine, Is there anyother algorithm which can generate random number?
0

Commented:
Is there a problem with using a guid?
0

Commented:
GUID is 128 bits, each generated GUID is not guaranteed to be unique, and GIUD are not random
0

Commented:
The chance of a guid collision is only of theoretical interest.

Also ANY random number here will have a chance of duplication approx 1/2^64. Its not theoretically possible to not have this chance and be random at the same time based upon the generally accepted definition of random.
0

Commented:
I interpreted "random" to mean "having no readily apparent pattern" since mathematically random would contradict "unique"
But it's hard to interpret "64" to mean"128"
0

Commented:
The question was posed just at that, is the only problem with 128 instead of 64 the 4 bytes of transfer or is there some other reason. Guids are kind of the defacto industry standard for the described problem. For things like long term maintenance I would probably use the Guid but thats me.

As to having no readily apparent pattern, I think that anything you do here will have a readily apparent pattern or it will have a possibility of collision. The moment you bring anything thats probabilistic into the equation you lose the assurance of no collision (you logically have to unless you are using a larger domain of results than you have in your original domain).

If you want probabilistic numbers you would probably have to check against the numbers that you have (or run some low probability of failure).

Cheers,

Greg

0

Commented:
The pattern in a DES encryption of a sequential number would be computationally infeasible to detect.
If that is still too apparent, there are stronger encryption methods that can be used.
0

Graphics ExpertCommented:
Hi,

If you want to be sure about uniqueness, then you should save each new random number you create, to make it possible to check if it is unique.
If so, lets name LIST where you store each number. Initially, LIST is empty. You must determine its size, say, to store 10,000 numbers or even 2^64 numbers!
A very simple algorithm for the function follows:

int NewNumber(ListSize, LIST)
{
valid = false
while not valid
{
N64 = CreateRandomNumber64(); // external random number generator
if N64 not in LIST
{
// optionally check for overlow in LIST. If so extend it.
valid = true
}
}
return N64
}

As external random generator, you can run c    rand   function, then add the time, multiply for the lenght of user's name, add the string of his/her city, and so on, such that you have a very randomic 64 bits number...

Jose
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.