Search code examples
c++matrixrandomclion

Can I clear default_random_engine generator after each use?


1) pre-processor directives

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

2) Creating a function to generate a random value within a given range 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

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

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. 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. 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

Solution

  • std::default_random_engine is implementation defined. Depending on the C++ standard library:

    • default_random_engine might seed the engine with a fixed value upon creation, or it might not.
    • default_random_engine might produce the same sequence of numbers if a seed is not given, or it might not.
    • default_random_engine might be defined as one of the C++ standard engines, or it might not.

    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 (e.g., RealGenerator(double, double, default_random_engine&)).