使用boost :: random并获取相同的数字序列 [英] Using boost::random and getting same sequence of numbers
问题描述
我有以下代码:
B类{
void generator $ b {
//创建随机数生成器
boost :: mt19937 randgen(static_cast< unsigned int>(std :: time(0)));
boost :: normal_distribution< float>噪声(0,1);
boost :: variate_generator& boost :: mt19937,
boost :: normal_distribution< float> > nD(randgen,noise);
for(int i = 0; i <100; i ++)
{
value = nD();
// graph each value
}
}
};
A类{
void someFunction()
{
for(int i = 1; i <= 3; i ++)
{
std :: shared_ptr< B> b;
b.reset(new B());
b-> generator();
}
}
};
我想快速执行上面的代码多次,以产生多个图。我还审查了此 stackoverflow问题,这是相似的,但警告状态当使用time(0)并且快速连续调用成员函数时,你仍然可能得到相同的数字序列。
我如何克服这个问题?
编辑:我已经尝试在B类中使用randgen静态,也尝试将其作为A类中的全局变量,但每次3个图仍然相同。我也试过播种从GetSystemTime毫秒。一种方法是不要在每次执行代码时对随机数生成器重新设定种子。
创建生成器并将其种子一次,然后继续使用。
这假设你在同一个运行中多次调用该代码。如果您要进行多次运行(但仍在同一秒内),则可以使用其他不同的属性(例如进程ID)更改种子。
可以使用Windows GetSystemTime()
返回一个SYSTEMTIME结构,其中一个元素为毫秒,或者Linux getTimeOfDay
返回自纪元以来的微秒数。
Windows:
#include< windows.h>
SYSTEMTIME st;
GetSystemTime(& st);
//使用st.wSecond * 100 + st.wMillisecs种子(0到59999)。
Linux:
code> #include< sys / time.h>
struct timeval tv;
gettimeofday(& tv,NULL);
//使用tv.tv_sec * 100 +(tv.tv_usec / 1000)种子(0到59999)。
I have the following code:
Class B {
void generator()
{
// creating random number generator
boost::mt19937 randgen(static_cast<unsigned int>(std::time(0)));
boost::normal_distribution<float> noise(0,1);
boost::variate_generator<boost::mt19937,
boost::normal_distribution<float> > nD(randgen, noise);
for (int i = 0; i < 100; i++)
{
value = nD();
// graph each value
}
}
};
Class A {
void someFunction()
{
for(int i = 1; i <=3; i++)
{
std::shared_ptr<B> b;
b.reset(new B());
b->generator();
}
}
};
I wish to execute the above code multiple times in rapid succession to produce multiple graphs. I have also reviewed this stackoverflow question which is similar but the caveat states that when time(0) is used and the member function is called in rapid succession then you will still likely get the same sequence of numbers.
How might I overcome this problem?
EDIT: I've tried making randgen static in Class B, also tried making it a global variable in Class A, but each time the 3 graphs are still the same. I've also tried seeding from the GetSystemTime milliseconds. I must be missing something.
One way would be to not reseed the random number generator every time you execute your code.
Create the generator and seed it once, then just continue to use it.
That's assuming you're calling that code multiple times within the same run. If you're doing multiple runs (but still within the same second), you can use another differing property such as the process ID to change the seed.
Alternatively, you can go platform-dependent, using either the Windows GetSystemTime()
returning a SYSTEMTIME structure with one of its elements being milliseconds, or the Linux getTimeOfDay
returning number of microseconds since the epoch.
Windows:
#include <windows.h>
SYSTEMTIME st;
GetSystemTime (&st);
// Use st.wSecond * 100 + st.wMillisecs to seed (0 thru 59999).
Linux:
#include <sys/time.h>
struct timeval tv;
gettimeofday (&tv, NULL);
// Use tv.tv_sec * 100 + (tv.tv_usec / 1000) to seed (0 thru 59999).
这篇关于使用boost :: random并获取相同的数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!