问题
I would like to generate unique 64-bit keys (pseudo)-randomly to identify objects in our model. I need the keys to be as unique as possible (minimize the probability of collisions when any N keys are used together) across all users of the system.
Usual GUIDs are out of the question for now because we're data cheap :). Because I don't foresee needing more than 1 million keys used in the same context, I would think 64-bit is enough (probability of collision would be about ~10e-7).
As a side-note, I will also need a scheme to fold/hash tuples of those keys into a single 64-bit key that also needs to be well distributed/unique.
Since I need a good (well distributed) hashing function anyway, would it be ok to fold a GUID in half (maybe accounting for the fixed bits in a GUID in some way)? Or is it better to use a local RNG? How would I seed the RNG to maximize uniqueness across space/time of generation? How strong a RNG would I need?
I'm not particularly looking for efficiency (up to a point) but I'd really like to ensure that the probabilities hold up their promise!
回答1:
hash a counter using a fast 128bit cryptographic hash like md5 and then split into two. that will give you "random", independent values that are 64bits each, and it should be pretty efficient.
and are you sure you can't use a simple counter?
update if you need a distributed solution, simply place a counter on each machine and hash the MAC address of the machine plus the counter. for better throughput use multiple counters per machine, each with a different name (A, B etc), and hash the name too. this is the big advantage of using hashes - you can throw anything in there. just be careful not to have ambiguities (for example, put "-" between each thing you hash, so that a name of "1" plus a count of "23" is not confused with a name of "12" and a count of "3").
来源:https://stackoverflow.com/questions/7243552/how-to-generate-unique-64-bit-keys