简体   繁体   English

如何重置Boost :: variate_generator分配?

[英]How to reset Boost::variate_generator distribution?

I am trying to write a function that takes in a min and a max and returns a random double between them. 我正在尝试编写一个接受最小值和最大值并在它们之间返回随机双精度数的函数。 I was trying to use Boost::variate_generator to get a random number between two doubles, but the issue is that I can't change the distribution on it, so I would have to make a new seed each call. 我试图使用Boost :: variate_generator来获取两个双打之间的随机数,但是问题是我无法更改其分布,因此每次调用我都必须创建一个新种子。 It defeats the purpose of a Pseudo Random Number Generator if I make a new seed every time I call it. 如果每次我叫一个新种子时,它就不能达到伪随机数发生器的目的。

Is there a way to get something like this below, Boost isn't necessary, it just seems to give good results. 有没有办法在下面得到这样的东西,Boost不是必需的,它似乎给出了很好的结果。

double getRandom(double min, double max);
{
    return randomNumberBetweenMinAndMax;
}

There are a lot of ways to do it. 有很多方法可以做到这一点。 You could have an argument with a default parameter that defaults to some globally available object. 您可以使用带有默认参数的参数,该参数默认为某些全局可用的对象。 You could have a locally scoped static variable that stores the generator. 您可能具有存储生成器的本地范围内的静态变量。 You could have a file scope static variable (similar to a class static if this were in a class) that stores the generator. 您可以有一个存储生成器的文件范围静态变量(如果在类中,则类似于类static)。 You could have a singleton generator that the function acquires. 您可能具有该函数获取的单例生成器。

I am partial to default parameters with a globally available object as the default. 我偏爱默认参数,并且默认使用全局可用的对象。 Its generally thread-safe as long the generator is thread safe and you initialize is before any threads are created. 只要生成器是线程安全的,并且在创建任何线程之前进行初始化,它通常都是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM