简体   繁体   English

我可以在每次使用后清除 default_random_engine 生成器吗?

[英]Can I clear default_random_engine generator after each use?

1) pre-processor directives 1) 预处理器指令

#include <iostream>
#include <random>
#include <math.h>

2) Creating a function to generate a random value within a given range a -> b 2) 创建一个 function 以生成给定范围 a -> b 内的随机值

double RandomRealGenerator(double a, double b)
{
    std::default_random_engine generator;
    std::uniform_real_distribution<double> realDistribution(a, b);

    return realDistribution(generator);
}

3) Function to create the matrice with the random values generated above as elements 3) Function 以上面生成的随机值作为元素创建矩阵

double arrays()
{
    // Declare a 3x3 matrix
    double MatrixA[3][3];

    // Give random values to each element in MatrixA
    MatrixA[0][0] = RandomRealGenerator(0, 1);  // element 1,1
    MatrixA[0][1] = RandomRealGenerator(0, 1);  // element 1,2
    MatrixA[0][2] = RandomRealGenerator(0, 1);  // element 1,3
    MatrixA[1][0] = RandomRealGenerator(0, 1);  // element 2,1
    MatrixA[1][1] = RandomRealGenerator(0, 1);  // element 2,2
    MatrixA[1][2] = RandomRealGenerator(0, 1);  // element 2,3
    MatrixA[2][0] = RandomRealGenerator(0, 1);  // element 3,1
    MatrixA[2][1] = RandomRealGenerator(0, 1);  // element 3,2
    MatrixA[2][2] = RandomRealGenerator(0, 1);  // element 3,3

    std::cout << MatrixA[0][0] << "  " << MatrixA[0][1] << "  " << MatrixA[0][2] << std::endl;
    std::cout << MatrixA[1][0] << "  " << MatrixA[1][1] << "  " << MatrixA[1][2] << std::endl;
    std::cout << MatrixA[2][0] << "  " << MatrixA[2][1] << "  " << MatrixA[2][2] << std::endl;
    std::cout << "\nIt's still not generating random doubles\n";
}

4) I keep getting a matrice where all of the elements are the same 4)我不断得到一个所有元素都相同的矩阵

0.0850324  0.0850324  0.0850324
0.0850324  0.0850324  0.0850324
0.0850324  0.0850324  0.0850324

5) I'm not a computer science major and I usually work in Python so if there is something simple I am grossly overlooking I would appreciate it if one could at least point me in the right direction. 5)我不是计算机科学专业的,我通常在 Python 工作,所以如果有一些简单的事情我会严重忽视,如果有人至少能指出我正确的方向,我将不胜感激。 Thanks.谢谢。

UPDATE更新

6) this worked but I would still like to be able to have a quick function I can grab to generate a random number between whatever range I would like in the moment. 6)这行得通,但我仍然希望能够有一个快速的 function 我可以抓住以在我想要的任何范围之间生成一个随机数。 I am using random values within various ranges to explore solutions to calculations within a given mathematical space.我正在使用各种范围内的随机值来探索给定数学空间内的计算解决方案。 I am doing this to collect data on valid values for free parameters in certain string models.我这样做是为了收集某些字符串模型中自由参数的有效值的数据。 Thanks again for your help.再次感谢你的帮助。

// Declare a 3x3 matrix
double MatrixA[3][3];
std::default_random_engine generator;
std::uniform_real_distribution<double> realDistribution(0, 1);

// Give random values to each element in MatrixA
MatrixA[0][0] = realDistribution(generator);  // element 1,1
MatrixA[0][1] = realDistribution(generator);  // element 1,2
MatrixA[0][2] = realDistribution(generator);  // element 1,3
MatrixA[1][0] = realDistribution(generator);  // element 2,1
MatrixA[1][1] = realDistribution(generator);  // element 2,2
MatrixA[1][2] = realDistribution(generator);  // element 2,3
MatrixA[2][0] = realDistribution(generator);  // element 3,1
MatrixA[2][1] = realDistribution(generator);  // element 3,2
MatrixA[2][2] = realDistribution(generator);  // element 3,3

std::default_random_engine is implementation defined. std::default_random_engine是实现定义的。 Depending on the C++ standard library:取决于 C++ 标准库:

  • default_random_engine might seed the engine with a fixed value upon creation, or it might not. default_random_engine可能会在创建时使用固定值为引擎播种,也可能不会。
  • default_random_engine might produce the same sequence of numbers if a seed is not given, or it might not.如果没有给出种子, default_random_engine可能会产生相同的数字序列,也可能不会。
  • default_random_engine might be defined as one of the C++ standard engines, or it might not. default_random_engine可能被定义为 C++ 标准引擎之一,也可能不是。

To achieve what you want (generate random real numbers in a range), pass an engine , not a distribution, to a function meant for this purpose (eg, RealGenerator(double, double, default_random_engine&) ).要实现您想要的(生成范围内的随机实数),请将engine而非分布传递给用于此目的的 function (例如RealGenerator(double, double, default_random_engine&) )。

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

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