I'm trying to use System.Random in F# but keep getting duplicate values. I'm aware that if I create a new instance of System.Random each time I do .Next i might get duplicate values. What I'm trying to achive is setting a random id on each record when creating them. Here is an example of the problem in code:
[<AutoOpen>]
module Domain =
type Test = {id: int; ....}
module Test =
let r = System.Random()
let t create =
{id = r.Next(); ....}
let a = Test.create
let b = Test.create
In the code above bot a
and b
gets the same id.
What I'm guessing is happening is that let r = System.Random()
doesn't get treated as a variable but as a function which returns a new instance of Random. Is this correct? And how can I Solve this problem?
Thanks!
Aucun commentaire:
Enregistrer un commentaire