我有以下代码:
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<floa
对c++来说是新手。我想创建一个类似于roll_die(unsigned int seed = ???)的函数,默认情况下,该函数在返回值之前使用srand(time(NULL))来为RNG添加种子,但也允许用户指定种子。这就是我想要的
#include <iostream>
#include <ctime>
int roll_die(unsigned int seed = 0){
// Returns a random integer between 1 and 6
// time(NULL) is the number of seconds sin
我希望能够这样做(显然不是有效的C++):
rng1 = srand(x)
rng2 = srand(y)
//rng1 and rng2 give me two separate sequences of random numbers
//based on the srand seed
rng1.rand()
rng2.rand()
有没有办法在C++中做这样的事情?例如,在Java语言中,我可以用我想要的种子创建两个java.util.Random对象。在C++中似乎只有一个全局随机数生成器。我相信有一些库可以提供这种功能,但不管怎样,只用C++就行了吗?
我正在做一些随机数的生成,并得到了可疑的行为。下面是我的代码:
// initialized earlier... in the constructor of a class
boost::mt19937 *rng = new boost::mt19937();
rng->seed(time(NULL));
// actual use here.
for (int i = 0; i < 10; ++i)
{
test();
}
void test()
{
boost::nor
最近,我正在试用一个在C++中使用#include<random>中定义的random engines生成随机数的程序。我的程序如下:-
#include <iostream>
#include <random>
#include <chrono>
using namespace std;
int random (int lim)
{
default_random_engine dre (chrono::steady_clock::now().time_since_epoch().count());
uniform_int_dis
为了测试,我需要可重复的随机数。随机性没那么重要。目前,我正在设置一个种子std::srand(x),并使用std::rand()生成。正如预期的那样,在具有相同种子的多次运行中,数字总是相同的。
对于其他部分,我需要UUID。我使用了utils-linux libuuuid。
但是运行以下代码:
std::srand(2);
uuid_t uuid;
int x = std::rand();
uuid_generate(uuid);
int y = std::rand();
y在每次运行中都是不同的。
我已经尝试过使用uuid_generate_time,uuid
下面的简单脚本并行地生成随机数
#include <random>
#include <iostream>
#include <omp.h>
int main(int argc, char *argv[])
{
// Three integers are expected from the command line.
// The first integer is a random seed
// The second integer is the number of threads.
// The third integer indica
我想知道为什么在程序的开始阶段使用srand比在我使用它的地方更有优势。
我在程序开始时生成伪随机数,但是当我在调用srand生成数字的函数中播种srand时,我同样会得到这些数字。
#include <iostream>
#include <ctime>
using namespace std;
int rng()
{
const int SIZE = 10;
int rng[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
rng[i] = ran