为什么这4个不同的随机数生成器函数生成相同的数字序列? [英] Why do these 4 different random number generator functions produce the same series of numbers?

查看:71
本文介绍了为什么这4个不同的随机数生成器函数生成相同的数字序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试的神经进化程序不会在每次调用时产生随机值.在程序中,使用以下语句初始化网络对象的向量:

A neuroevolution program I am in the process of debugging does not produce random values every time it is called. In the program, a vector of Network objects are initialized with the following statement:

vector<Network> population(POPULATION_SIZE, Network(sizes, inputCount));

为什么我认为该计划不能收敛到最佳解决方案的原因在于,总是前100个人口是相同的.当以这种方式初始化网络时,连接权重和神经元偏差(每个)都使用以下类函数进行初始化:

Why I believe the program not to be converging to an optimal solution is that, always, the first 100 of the population are the same. When a network is initialized in this manner, the connection weights and neuron biases are (each) initialized with the following class function:

double Network::randDouble(double low, double high) {
    /*default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
    uniform_real_distribution<double> distribution(low, high);
    return distribution(generator);*/

    /*srand(time(NULL));
    double temp;
    if (low > high) {
        temp = low;
        low = high;
        high = temp;
    }
    temp = (rand() / (static_cast<double>(RAND_MAX) + 1.0)) * (high - low) + low;
    return temp;*/

    /*mt19937 rgn(std::chrono::system_clock::now().time_since_epoch().count());
    uniform_real_distribution<double> gen(low, high);
    return gen(rgn);*/

    default_random_engine rd;
    uniform_real_distribution<double> gen(low, high);
    auto val = std::bind(gen, rd);
    return val();
}

3个注释掉的部分是以前尝试生成所需功能的方法.在每种情况下,它们为每个网络产生相同的数字(从1个权重到另一个权重,而不是1个网络到另一个权重).尝试的方法基于此处的答案:

The 3 commented-out sections are previously attempted means of generating the functionality required. In each case, they produce the same numbers for each network (differing from 1 weight to another, but not 1 network to another). The methods attempted are based on answers from here:

  1. c ++-default_random_engine创建所有同一系列的时间
  2. http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution

此外,第二种方法在有或没有种子的情况下都能产生相同的结果.我肯定错过了什么.

In addition, the second method produces the same results with or without the seed. I must be missing something.

另一个可能无关紧要的问题是,使用OpenMP可以并行化使用此功能的功能,并且当并行调用时,结果可能是相同的.

Another, albeit potentially irrelevant concern, is that functions using this may be parallel-ized using OpenMP, and that when called in parallel, the results could be the same.

推荐答案

您的问题是,每次生成数字时,您都要初始化(播种)随机生成器.在简单的 srand()情况下,您应该在程序启动期间仅调用一次 srand(),然后在每次需要时调用 rand()一个数字.在更复杂的情况下,您应该只构造一次生成器(在整个程序运行中),并根据需要多次使用它.

Your problem is that you are initializing (seeding) the random generator every time you generate a number. In the simple srand() case, you should call srand() just once during program start, then call rand() every time you need one number. In the more complex cases, you should construct the generator just once (in the entire program run), and use it as many times as you need.

这篇关于为什么这4个不同的随机数生成器函数生成相同的数字序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆