lundi 7 novembre 2016

F# System.Random in a Recursive function doing funny stuff

I have this funktion that chooses 4 randoms colors and makes a list of it. Atleast i want it too.:

let theList = [Red;Green;Yellow;Purple;White;Black]
let rec a x =
  let rnd = System.Random()
  match x with 
  |0 -> []
  |_ -> (List.item(rnd.Next(6)) theList)::(a (x-1)) 

The problem is though it picks a random color every time i run the funktion then it always picks the same color for the entire list. [Red;Red;Red;Red] or [Green;Green;Green;Green] etc.

It is a mystery to me how it can arrive at the same color everytime it makes the recursive call.

If i use the random method in a for loop then no problem.

Can someone explain to me what is happening here?




Aucun commentaire:

Enregistrer un commentaire