gpt4 book ai didi

random - System.Random 产生相同的随机数

转载 作者:行者123 更新时间:2023-12-04 13:15:32 29 4
gpt4 key购买 nike

尽管随机生成器只创建一次,但输出始终是相同的随机结果(对于所有三个测试输出)。

来自稍大脚本的测试片段:

   let myRandGen = System.Random()
let getRandomObject =
let options = [|"Bob"; "Jim"; "Jane"|]
let randIndex = myRandGen.Next(options.Length)
options.[randIndex]

printfn "New one: %s" getRandomObject
printfn "New two: %s" getRandomObject
printfn "New three: %s" getRandomObject

我需要每次调用的输出都是随机的,而目前不是。

示例输出:
New one: Jane
New two: Jane
New three: Jane

最佳答案

您的 getRandomObject是一个值。它被评估一次。要解决此问题,请制作 getRandomObject一个功能:

let getRandomObject () = 
let options = [|"Bob"; "Jim"; "Jane"|]
let randIndex = myRandGen.Next(options.Length)
options.[randIndex]

并像这样称呼它: getRandomObject ()

关于random - System.Random 产生相同的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310882/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com