mardi 26 juillet 2016

F# using System.Random() to get random number lists give the same lists

I have been searching other questions, where users would instantiate many System.Random()'s within a loop or method and therefore create many of the same randoms from the same clock. But here I have one instantiated System.Random() but when i try to use it to create multiple random number lists, they are all the same.

module Scripts =
let rnd = System.Random() 

let getRandom36 =
    let rec generate (l : list<int>) =
        match l.Length with
        |8 -> l
        |_ -> let number = rnd.Next 38
              if(List.exists(fun elem -> elem=number) l) then generate l else generate (number::l)
    List.sort(generate List.empty)

let myseq = Seq.init 4 (fun _ -> getRandom36)

The important part is not really how the code inside getRandom36 works, I have been tampering with it to work in different ways, but I keep getting lists that look the same when calling myseq;;.

myseq;;
val it : seq<int list> =
  seq
    [[2; 8; 10; 11; 18; 21; 22; 35]; [2; 8; 10; 11; 18; 21; 22; 35];
     [2; 8; 10; 11; 18; 21; 22; 35]; [2; 8; 10; 11; 18; 21; 22; 35]; ...]

Any ideas why? I mean shouldnt the rnd.Next be different each time since no new instance of rnd is made for each iteration.




Aucun commentaire:

Enregistrer un commentaire