Generating unique random numbers

karczma at karczma at
Tue Jun 14 11:30:50 EDT 2005

This topic should be posed to a more general forum than GHusers... 

Jens Fisseler answers: 

>> Is it possible to generate random numbers that are "unique"  in O(1) in 
>> Haskell?

>> Of course, I could create unique numbers by incrementing a global value, 
>> but for security reasons I need random numbers.
> do you really need random numbers? If not: I vaguely remember an
> algorithm from one of the "Graphic Gems" books, implementing some sort
> of fancy counter which goes through all numbers from 0,...,2^n-1 in a
> seemingly random, but of course deterministic and repeatable order.

Actually, decent random number generators, even simple congruential ones: 

X_{n+1} = A*X_n + C (mod M) 

if the parameters are chosen correctly, *guarantee* to generate ALL M
integers between 0 and M-1 without repetitions, and only then the sequence
repeats. Take M sufficiently large, and you are safe, provided you know
what you are doing. See Knuth for details, how to choose the params. 

Another story -- of course -- is the generation of RN in Haskell. Too
often, too many people suggest all these horrible manadic devices,
while the usage of a simple lazy stream of consecutively generated numbers
would suffice... 

Bon courage. 

Jerzy Karczmarczuk 

More information about the Glasgow-haskell-users mailing list