Generate multiple random numbers in haskell

1) System.Random.randoms

Simplest way to generate an infinite list of random numbers.

Internally, randoms construct an infinite list:

randoms g = (\(x,g') -> x : randoms g') (random g)

2) State monad

The previous method is simple, but doesn't return a generator, so effectively, the generated numbers are the only one available. To get some random variable and a new generator, the state monad can be used.

This approach also lends itself to more complex operation, for example, returning a tuple:

3) Simplifing with state

The genRng function is a bit verbose and can be simplified with state

This approach is much nicer when returning tuple for example:

This way, no need to do any bookkeeping with the internal state.

Here are three ways to generate multiple random values. These method works for any kind of stateful computation. The monad approach gives more control, allowing any function like s -> (a, s) to be embedded inside, abstracting away any work to thread the state across calls. The State monad can also be generalized using monad transformer. Some good read about the transformer approach: