boost::random 每次生成相同的数字
main .cpp
#include "stdafx.h"
#include "random_generator.h"
int
main ( int argc, char *argv[] )
{
cout.setf(ios::fixed);
base_generator_type base_generator;
int max = pow(10, 2);
distribution_type dist(1, max);
boost::variate_generator<base_generator_type&,
distribution_type > uni(base_generator, dist);
for ( int i=0; i<10; i++ ) {
//cout << random_number(2) << endl;
cout << uni() << endl;
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
random_gemerator.h
#include "stdafx.h"
#include <boost/random.hpp>
#include <boost/generator_iterator.hpp>
typedef boost::mt19937 base_generator_type;
typedef boost::lagged_fibonacci19937 fibo_generator_type;
typedef boost::uniform_int<> distribution_type;
typedef boost::variate_generator<fibo_generator_type&,
distribution_type> gen_type;
int
random_number ( int bits )
{
fibo_generator_type fibo_generator;
int max = pow(10, bits);
distribution_type dist(1, max);
gen_type uni(fibo_generator, dist);
return uni();
} /* ----- end of function random_number ----- */
stdafx.h
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
每次运行它时,它都会生成相同的数字序列,
如 77, 33,5, 22 , ...
如何正确使用 boost:random ?
就是这样。但可能有一个小问题,如下所示:
听起来
get_seed(); for (;;) {cout << generate_random() << endl; } // is ok
它会生成相同的随机数
int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;} // output the same random number yet
main .cpp
#include "stdafx.h"
#include "random_generator.h"
int
main ( int argc, char *argv[] )
{
cout.setf(ios::fixed);
base_generator_type base_generator;
int max = pow(10, 2);
distribution_type dist(1, max);
boost::variate_generator<base_generator_type&,
distribution_type > uni(base_generator, dist);
for ( int i=0; i<10; i++ ) {
//cout << random_number(2) << endl;
cout << uni() << endl;
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
random_gemerator.h
#include "stdafx.h"
#include <boost/random.hpp>
#include <boost/generator_iterator.hpp>
typedef boost::mt19937 base_generator_type;
typedef boost::lagged_fibonacci19937 fibo_generator_type;
typedef boost::uniform_int<> distribution_type;
typedef boost::variate_generator<fibo_generator_type&,
distribution_type> gen_type;
int
random_number ( int bits )
{
fibo_generator_type fibo_generator;
int max = pow(10, bits);
distribution_type dist(1, max);
gen_type uni(fibo_generator, dist);
return uni();
} /* ----- end of function random_number ----- */
stdafx.h
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
every time I run it, it all generate the same number sequence
like 77, 33,5, 22 , ...
how to use boost:random correctly?
that is it. but maybe have a little problem, like the following:
it seems sound
get_seed(); for (;;) {cout << generate_random() << endl; } // is ok
it genereate the same random number
int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;} // output the same random number yet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您希望每次运行程序时随机数序列都发生变化,则需要通过使用当前时间初始化来更改随机种子,例如
您将找到一个示例 那里,摘录:
if you want the sequence of random numbers to change every time you run your program, you need to change the random seed by initializing it with the current time for instance
you will find an example there, excerpt:
您需要为随机数生成器提供种子,这样它就不会每次都从同一个地方开始。
根据您对数字的处理方式,您可能需要考虑如何选择种子值。如果您需要高质量的随机性(如果您正在生成加密密钥并希望它们相当安全),您将需要一个良好的种子值。如果这是 Posix,我会建议 /dev/random - 但你看起来使用的是 Windows,所以我不确定什么是好的种子源。
但如果您不介意可预测的种子(用于游戏、模拟等),则快速且肮脏的种子是 time() 返回的当前时间戳。
You need to seed your random number generator so it doesn't start from the same place each time.
Depending on what you are doing with the numbers, you may need to put some thought into how you choose your seed value. If you need high quality randomness (if you are generating cryptographic keys and want them fairly secure), you will need a good seed value. If this were Posix, I would suggest /dev/random - but you look to be using Windows so I'm not sure what a good seed source would be.
But if you don't mind a predictable seed (for games, simulations, etc.), a quick and dirty seed is the current timestamp returned by time().
如果您在 'nix 系统上运行,您总是可以尝试这样的事情;
我猜想以这种方式播种随机数生成器比简单地读取
/dev/urandom
(或/dev/random
)来满足您的所有随机数需求要快。If you are running on a 'nix system, you could always try something like this;
I'm guessing seeding the random number generator this way is faster than simply reading the
/dev/urandom
(or/dev/random
) for all your random number needs.您可以使用 boost::random::random_device 按原样分类,或者为其他生成器提供种子。
您可以通过简单的操作获得一个一次性随机数:
You can use the boost::random::random_device class either as-is, or to seed your other generator.
You can get a one-off random number out of it with a simple: