我想洗牌的列表的元素相同的结果: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工作正常,我,但我很好奇,为什么这种行为显示出来)