English 中文(简体)
这是否是一种适当的read子-sa子 Rand子?
原标题:Is this a proper thread-safe Random wrapper?

我对阅读和同意相当不感兴趣;对此,我目前正在努力在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.

这是否正确? 是否有明显的问题?

最佳回答

I think your approach is pretty reasonable - using ThreadLocal gives you safe access to the Random and using a master random number generator to provide seeds means that you ll get random values even if you access it from multiple threads at similar time. It may not be random in the cryptographical sense, but should be fine for most other applications.

至于测试,这是很trick的。 如果<代码>Random的中断,它将恢复时间,但这种经验是公正的,很难说你需要多少时间才能安全地利用。 我所能建议的最佳方法是实施一些简单的随机测试(例如simples,在 lo中从多个深层进入,尽管这种检测仍然很不正确,因为每次都不会失败。

另外,你不需要使用<条码>的<>类型>来概述这种行为。 也可作为一种职能撰写:

open System
open System.Threading

module Probability =

   let Draw =
     // Create master seed generator and thread local value
     let seedGenerator = new Random()
     let localGenerator = new ThreadLocal<Random>(fun _ -> 
       lock seedGenerator (fun _ -> 
         let seed = seedGenerator.Next()
         new Random(seed)))
     // Return function that uses thread local random generator
     fun () ->
       localGenerator.Value.NextDouble()
问题回答

这感觉是错误的。 为什么不只使用一个单一州(只创造了一个兰芒州,并锁定它)?

如果实际随机性是令人关切的问题,那么可能就见RNGCryptoserviceProvider, 后者是read。

除非出现业绩瓶颈,否则,我想像一些情况一样。

let rand = new Random()

let rnext() = 
     lock rand (
        fun () -> 
           rand.next())

更容易理解,但我认为,你的方法是罚款的。

If you really want to go with the OO approach, then your code may be fine (I won t say it is fine as I am not too smart to understand OO :) ). But in case you want to go the functional way it would be as simple as something like:

type Probability = { Draw : unit -> int }

let probabilityGenerator (n:int) = 
    let rnd = new Random()
    Seq.init n (fun _ -> new Random(rnd.Next()))
    |> Seq.map (fun r -> { Draw = fun () -> r.Next() })
    |> Seq.toList

Here you can use the function probabilityGenerator to generate as much as "Porbability" type object and then distribute them to various threads which can work on them in parallel. The important thing here is that we are not introducing lock etc in the core type i.e probability and it becomes the responsibility of the consumer how they want to distribute it across threads.





相关问题
Weighted random numbers

I m trying to implement a weighted random numbers. I m currently just banging my head against the wall and cannot figure this out. In my project (Hold em hand-ranges, subjective all-in equity ...

Comprehensive information about hash salts

There are a lot of questions about salts and best practices, however most of them simply answer very specific questions about them. I have several questions which feed into one another. Assuming a ...

Generate unique names?

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image. uniqid(rand(), true); and adding a large random ...

How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

热门标签