Settings

Theme

How to Generate a Sequence of Unique Random Integers (2012)

preshing.com

4 points by vince14 5 years ago · 1 comment

Reader

ChrisLomont 5 years ago

Use any period 2^32 PRNG, which can easily be done with 4 bytes of storage. He used 8.

A simple LCG is of the form

seed <- A*seed + B

Start with any seed, in 32 bit unsigned int, assume 32 bit wrapping like in C/C++, etc. (or do mod yourself), pick good A and B, and away you go.

The Hull–Dobell Theorem tells you how to pick A and B for this size m=2^32: A and B are both odd and larger than 1 and A-1 is divisible by 4.

Finding nice A and B is a bit tricky - in which case look online for good choices or related theory.

Of course, a LCG may not be as random as you like. In that case one of the PCG variants solve the problem elegantly.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection