随机数生成器:应该用作单例吗? [英] Random Number Generator: Should it be used as a singleton?

查看:277
本文介绍了随机数生成器:应该用作单例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个地方使用随机数,通常在需要时构造一个随机数生成器.目前,我使用Marsaglia Xorshift算法将其与当前系统时间一起作为种子. 现在,我对此策略有一些疑问: 如果我使用多个生成器,则生成器之间数字的独立性(随机性)取决于种子(相同种子的相同数字).由于我将时间(ns)用作种子,并且由于该时间发生了变化,因此我想知道是否仅使用一个奇异发生器和例如使它作为单例可用.这会增加随机数的质量吗?

I use random numbers in several places and usually construct a random number generator whenever I need it. Currently I use the Marsaglia Xorshift algorithm seeding it with the current system time. Now I have some doubts about this strategy: If I use several generators the independence (randomness) of the numbers between the generators depends on the seed (same seed same number). Since I use the time (ns) as seed and since this time changes this works but I am wondering whether it would not be better to use only one singular generator and e.g. to make it available as a singleton. Would this increase the random number quality ?

不幸的是c ++ 11还不是一个选择

Unfortunately c++11 is not an option yet

更具体地说:我并不是在建议单例可以提高随机数质量,而是建议仅使用一个生成器并对其进行播种.否则,我必须确保不同生成器的种子彼此独立(随机). 极端的例子:我给两个生成器编号完全相同->它们之间没有随机性

To be more specific: I am not suggesting that the singleton could increase the random number quality but the fact that only one generator is used and seeded. Otherwise I have to be sure that the seeds of the different generators are independent (random) from another. Extreme example: I seed two generators with exactly the same number -> no randomness between them

推荐答案

假设您有几个变量,每个变量都必须是随机变量,彼此独立,并且会定期使用来自某个随机数生成器的新随机值重新分配.这在蒙特卡洛分析和游戏中经常发生(尽管游戏的严谨性远低于蒙特卡洛的严谨性).如果存在完美的随机数生成器,则最好使用单个实例化.将生成器中的 n th 个伪随机数分配给变量 x 1 ,将下一个随机数分配给变量 x 2 x 3 的下一个,依此类推,最终返回到变量 x 1 在下一个周期.大约.这里存在一个问题:使用这种方式时,太多的PRNG未能通过独立性测试而未能通过独立性测试,有的甚至甚至失败了对单个序列的随机性测试.

Suppose you have several variables, each of which needs to be random, independent from the others, and will be regularly reassigned with a new random value from some random generator. This happens quite often with Monte Carlo analysis, and games (although the rigor for games is much less than it is for Monte Carlo). If a perfect random number generator existed, it would be fine to use a single instantiation of it. Assign the nth pseudo random number from the generator to variable x1, the next random number to variable x2, the next to x3, and so on, eventually coming back to variable x1 on the next cycle. around. There's a problem here: Far too many PRNGs fail the independence test fail the independence test when used this way, some even fail randomness tests on individual sequences.

我的方法是将单个PRNG生成器用作一组独立的PRNG实例的 N 个实例的种子生成器.这些后面的PRNG的每个实例都提供一个变量. 自包含"是指PRNG是一个对象,其状态保留在实例成员中,而不是在静态成员或全局变量中.种子生成器甚至不需要与其他 N 个PRNG属于同一家族.在多个线程同时尝试使用种子生成器的情况下,只需重新输入即可.但是,在我的使用中,我发现最好在线程开始之前设置PRNG,以确保可重复性.那是一次运行,一次执行.蒙特卡洛技术通常需要数千次执行,也许更多,也许更多.对于蒙特卡洛,可重复性至关重要.因此,还需要一个随机种子生成器.这一种子为用于生成变量的 N 生成器的种子生成器提供了种子.

My approach is to use a single PRNG generator as a seed generator for a set of N instances of self-contained PRNGs. Each instance of these latter PRNGs feeds a single variable. By self-contained, I mean that the PRNG is an object, with state maintained in instance members rather than in static members or global variables. The seed generator doesn't even need to be from the same family as those other N PRNGs. It just needs to be reentrant in the case that multiple threads are simultaneously trying to use the seed generator. However, In my uses I find that it is best to set up the PRNGs before threading starts so as to guarantee repeatability. That's one run, one execution. Monte Carlo techniques typically need thousands of executions, maybe more, maybe a lot more. With Monte Carlo, repeatability is essential. So yet another a random seed generator is needed. This one seeds the seed generator used to generate the N generators for the variables.

可重复性很重要,至少在蒙特卡洛世界中如此.假设长时间的蒙特卡洛模拟的运行编号10234导致某些重大故障.很高兴看到世界上发生了什么.这可能是统计上的fl幸,这可能是一个问题.问题在于,在典型的MC设置中,仅记录了最少的数据,仅足以计算统计数据.要查看运行10234发生了什么,需要重复该特定情况,但现在要记录所有内容.

Repeatability is important, at least in the Monte Carlo world. Suppose run number 10234 of a long Monte Carlo simulation results in some massive failure. It would be nice to see what in the world happened. It might have been a statistical fluke, it might have been a problem. The problem is that in a typical MC setup, only the bare minimum of data are recorded, just enough for computing statistics. To see what happened in run number 10234, one needs to repeat that particular case but now record everything.

这篇关于随机数生成器:应该用作单例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆