Solved

Posted on 2007-07-28

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.

9 Comments

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.

But it's hard to interpret "64" to mean"128"

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

If that is still too apparent, there are stronger encryption methods that can be used.

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

{

add N64 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

Title | # Comments | Views | Activity |
---|---|---|---|

Factors of Positive Integers | 4 | 243 | |

Storing historical updates in a database | 9 | 112 | |

With what software can I hide the algorithm that determines the validity of a password? The input will be from dos, powershell or HTA. | 17 | 156 | |

Info needed on Algorithmic trading | 1 | 78 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**22** Experts available now in Live!