我想洗牌的列表的元素相同的結果:System.Random()和Next()返回
(* Returns a list with the same elements as the original but in randomized order *)
let shuffle items =
items
|> List.map (fun x -> (x, System.Random().Next()))
|> List.sortBy snd
|> List.map fst
然而,這只是總是以相同的順序返回items
,因爲:
> List.map (fun x -> x, System.Random().Next()) [1; 2; 3];;
val it : (int * int) list = [(1, 728974863); (2, 728974863); (3, 728974863)]
> List.map (fun x -> x, System.Random().Next()) [1; 2; 3];;
val it : (int * int) list =
[(1, 1768690982); (2, 1768690982); (3, 1768690982)]
> List.map (fun x -> x, System.Random().Next()) [1; 2; 3];;
val it : (int * int) list = [(1, 262031538); (2, 262031538); (3, 262031538)]
爲什麼System.Random().Next()
總是在每個調用中返回相同的值?是否因爲連續的呼叫在時間上過於靠近?或者我以其他方式誤用API?
(注:此answer工作正常,我,但我很好奇,爲什麼這種行爲顯示出來)