I am trying to develop a random number “generator” in F#.
I successfully created the following function:
let draw () =
let rand = new Random()
rand.Next(0,36)
This works fine and it generates a number between 0 and 36.
However, I am trying to create a function that runs this function several times.
I tried the following
let multipleDraws (n:int) =
[for i in 1..n -> draw()]
However, I only get a single result, as draw is evaluated only once in the for comprehension.
How could I force multiple executions of the draw function?
The problem is with the Random type. It uses the computer’s time to generate a seed and then generate the random numbers. Since practically the time of the calls is identical, the same seed is generated and so are also same numbers returned.
This will solve your problem:
And then: