Solved

# Random Unique ID generate

Posted on 2007-07-28
1,345 Views
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
Question by:marchent

LVL 84

Accepted Solution

How about a DES of a sequentially allocated number?
0

LVL 13

Author Comment

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

LVL 37

Expert Comment

Is there a problem with using a guid?
0

LVL 84

Expert Comment

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

LVL 37

Expert Comment

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

LVL 84

Assisted Solution

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

LVL 37

Expert Comment

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

LVL 84

Expert Comment

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

LVL 18

Assisted Solution

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

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

#### Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!