简体   繁体   English

是否可以为boost :: random :: uniform_int_distribution <>设置确定性种子?

[英]Is it possible to set a deterministic seed for boost::random::uniform_int_distribution<>?

I'm using boost::random::uniform_int_distribution<boost::multiprecision::uint256_t> to generate some unit tests. 我正在使用boost::random::uniform_int_distribution<boost::multiprecision::uint256_t>生成一些单元测试。 Notice that I'm using multiprecision , which is why I need to use boost and not the standard library. 注意,我使用的是multiprecision ,这就是为什么我需要使用boost而不是标准库的原因。 For my periodic tests, I need to generate deterministic results from a nondeterministic seed, but in such a way where I can reproduce the results later in case the tests fail. 对于我的定期测试,我需要从不确定性种子中生成确定性结果,但是这种方式可以在测试失败的情况下稍后重现结果。

So, I would generate a true random number and use as a seed, and inject that to uniform_int_distribution . 因此,我将生成一个真实的随机数并将其用作种子,然后将其注入uniform_int_distribution The purpose is that if this fails, I'll be able to reproduce the problem with the same seed that made the tests fail. 目的是如果失败,我将能够使用导致测试失败的种子来重现该问题。

Does this part of boost support generating seed-based random numbers in its interface? Boost的这一部分是否支持在其界面中生成基于种子的随机数? If not, is there any other way to do this? 如果没有,还有其他方法吗?

The way I generate random numbers currently is: 我目前生成随机数的方式是:

boost::random::random_device                                              gen;
boost::random::uniform_int_distribution<boost::multiprecision::uint256_t> dist{100, 1000};
auto random_num = dist(gen);

PS: Please be aware that the primary requirement is to support multiprecision . PS:请注意,主要要求是支持multiprecision I require numbers that range from 16 bits to 512 bits. 我需要的数字范围是16位到512位。 This is for tests, so performance is not really a requirement. 这是为了测试,因此性能并不是真正的要求。 I'm OK with generating large random numbers in other ways and converting them to boost::multiprecision . 我可以通过其他方式生成较大的随机数并将其转换为boost::multiprecision

The boost::random::random_device is a Non-deterministic Uniform Random Number Generator , a true random number generator. boost::random::random_device是一个Non-deterministic Uniform Random Number Generator ,它是一个真正的随机数生成器。 Unless you need real non-deterministic random numbers you could use a Pseudo-Random Number Generator (at least for testing purposes), which can be seeded. 除非需要真正的不确定性随机数,否则可以使用Pseudo-Random Number Generator (至少出于测试目的),可以将其作为种子。 One known Pseudo-Random Number Generator is the mersenne twister boost::random::mt19937 . 一种已知的伪随机数生成器是mersenne扭曲器boost::random::mt19937

This generator usually gets seeded by a real random number which you could print for reproducability in your unit tests: 该生成器通常由一个真实的随机数作为种子,您可以打印该随机数以提高单元测试的可重复性:

auto seed = boost::random::random_device{}();
std::cout << "Using seed: " << seed << '\n';
boost::random::mt19937 gen{ seed };
boost::random::uniform_int_distribution<boost::multiprecision::uint256_t> dist{100, 1000};
auto random_num = dist(gen);

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

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