我对阅读和同意相当不感兴趣;对此,我目前正在努力在F#中执行随机研究算法。 我在该系统周围写了一幅画面。 Random阶级,根据现有想法 C# 例子——但我不敢肯定,我会如何开始对这一错误行为进行联合测试,因此,我要听取更多经验的人的意见,如果由于F# syntax或深造的误解,我的法典有明显的缺陷或改进:
open System
open System.Threading
type Probability() =
static let seedGenerator = new Random()
let localGenerator =
new ThreadLocal<Random>(
fun _ ->
lock seedGenerator (
fun _ ->
let seed = seedGenerator.Next()
new Random(seed)))
member this.Draw() =
localGenerator.Value.NextDouble()
My understanding of what this does: ThreadLocal ensures that for an instance, each thread receives its own instance of a Random, with its own random seed provided by a common, static Random. That way, even if multiple instances of the class are created close in time, they will receive their own seed, avoiding the problem of "duplicate" random sequences. The lock enforces that no two threads will get the same seed.
这是否正确? 是否有明显的问题?