Random Numbers in Haskell
Haskell’s System.Random
module provides pseudorandom number generation.
To run the program, save it as random-numbers.hs
and use runhaskell
:
Some of the generated numbers may be different when you run the sample.
See the System.Random
module documentation for references on other random quantities that Haskell can provide.
In Haskell, we use the System.Random
module for random number generation. The randomRIO
function is used to generate random numbers within a specified range, similar to rand.IntN
in Go. For floating-point numbers, we use randomIO
and randomRIO
with appropriate type annotations.
To create a generator with a known seed, we use mkStdGen
instead of NewPCG
. The randomRs
function is then used to generate a list of random numbers from this generator.
Note that Haskell’s random number generation is pure, meaning that for a given seed, it will always produce the same sequence of random numbers. This is why we get the same output when using the same seed twice.