我正在尝试用随机数填充列表,但很难获得随机数部分。我现在打印出一个随机数 10 次,我想要的是打印出 10 个不同的随机数
let a = (new System.Random()).Next(1, 1000)
let listOfSquares = [ for i in 1 .. 10->a]
printfn "%A" listOfSquares
有什么提示或建议吗?
您的代码只是获取一个随机数并使用它十次。
这个扩展方法可能有用:
type System.Random with
/// Generates an infinite sequence of random numbers within the given range.
member this.GetValues(minValue, maxValue) =
Seq.initInfinite (fun _ -> this.Next(minValue, maxValue))
然后你可以像这样使用它:
let r = System.Random()
let nums = r.GetValues(1, 1000) |> Seq.take 10
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)