Reputation: 4445
i am trying to create unique random identifier strategy. I do not want to store or query all ids that were already used, I'd like to use as a source of uniquesness a sequence generating numbers 0 -> Integer.MAX_VALUE. The thing I am thing that I am missing is a function that will project each number from the sequence to another in the same range.
Can you please point to some fast bijection function for this? (Prefferably in Java).
Thanks in advance
Upvotes: 0
Views: 77
Reputation: 100209
If you want to produce "unguessable" random numbers, you can take SecureRandom.nextLong()
and append it with System.nanoTime()
. The nanoTime part is easier to guess, but will not repeat after very short period of time. The nextLong part may repeat in future, but it's very hard to guess.
Note that if you have any determenistic bijection function hardcoded in your application and somebody will be able to obtain and reverse it (get sources, decompile class files, etc.), then your IDs will be compromised: it would be quite easy to find next/previous numbers by given one.
Upvotes: 1