Generating Unique random integers
: Hi,
: I would like an efficient algorithm to generate unique random integers.
: Can someone please post such algorithm, or suggest a good reference?
: Thanx in advance,
: --MJ
Consider the following:
1. Call R(x) the xth (pseudo)random number, in sequence,
generated by your random number generator.
2. Initialize i to 0 and k to 1.
3. Generate R(i) and R(k).
4. R(i) is a unique random number.
5. If R(i) equals R(k) stop generating random numbers
because they have begun to cycle, i.e., the next
number generated will repeat a previous one.
Otherwise, continue on to step 6.
6. Add 1 to i, and add 2 to k.
7. Go to step 3.
If you write this up as a program, you may need to make
some range checks on the numbers.
-- Wetboy