[Haskell-cafe] A Random Question

jerzy.karczmarczuk at info.unicaen.fr jerzy.karczmarczuk at info.unicaen.fr
Sat Dec 15 06:40:35 EST 2007

Dominic Steinitz writes: 

> I need to generate distinct arbitrary values... 
> ... I'll create my own random number generator (which
> will not be random at all)
> ...

> myNext (MyGen s1) =
>    (s1, MyGen (s1 + 1))

> 2. Is this the right approach to generating predictable arbitrary
> values? Are there others?

The "really random" congruential generators: x -> A*x + C (mod M) are
fully predictable in the sense that there is no indeterminacy involved,

What do you need, i.e., what meaning do you attribute to the words
"predictable" and "arbitrary"? 

BTW. people often think that a "pure function" cannot generatate pseudo-
random numbers, that a generator *must* thread the updated seed. But
there are hashing functions, which for n=0,1,2,3,4,... generate a complete
mess, numbers which look random and uncorrelated. For example, if you do
that (a pseudocode, [actually, this code works in Clean, which discards 
overflows in Int arith.] not Haskell): 

n = (n<<13) `bitxor` n
return toReal(n*(n*n*599479+649657)+1376312589)/2147483648.0 

provided your favourite language is able to discard all overflows tacitly,
you will get "arbitrary" values, for sequential arguments. Of course,
predictable in the well understood sense, practically not easy to predict. 

Jerzy Karczmarczuk 

More information about the Haskell-Cafe mailing list