Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeding rand() for a C++ class

I am working on a C++ class that uses a rand() in the constructor. I would really like for this class to take care of itself in pretty much every way, but I'm not sure where to seed rand().

If I seed rand() in the constructor, it will be seeded every time a new instance of my object type is constructed. So if I were to create 3 objects in sequence, they would all be created in the same second and therefore have the same seed for rand(), producing the exact same data for each of the 3 instances of the object.

I would like to seed rand() within the class code, rather than having to do it in the main function of my program before I create the object. I thought of doing a static bool seeded; variable that signifies whether or not rand() has been seeded yet, but I'm not really sure how to initialize it to false at the creation of the class.

My idea goes something like

myConstructor(){
    if(!seeded){
        srand(time(NULL));
        seeded = true;
    }

    myVariable = rand()%maxVal;
}

I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program. It is my understanding that changing this static value to true would carry across all instances of the object if it were static, and would therefore only execute the seed function the very first time that object type is created.

like image 854
Alex Avatar asked Nov 04 '12 02:11

Alex


2 Answers

I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program.

// my_class.h
class my_class {
public:
  // ...
private:
  static bool seeded;
};

// my_class.cpp
bool my_class::seeded = false;

Make sure to define seeded in the implementation file. Otherwise every file which includes your header will get its own definition of the static member and it could also cause linker issues as it can be defined more than once.

On a side note, if the static member were of a const integral type, you could assign it at the point of declaration.

Another option would be this, and personally I would prefer it for this task:

my_class::my_class()         
{
    static bool seeded = false;
    if(!seeded) {
        srand(time(NULL));
        seeded = true;
    }

    myVariable = rand() % maxVal;
}
like image 116
Ed S. Avatar answered Sep 22 '22 21:09

Ed S.


This issue is one of the problems using rand(). C++11 introduced the <random> library which solves this and other issues.

Instead of having a single global (or per thread) state for rand() the new API gives you explicit control over the state of a RNG by encapsulating it in an object with value semantics.

You could maintain the state as a member variable, or as a static member if you want all instances to share one, or whatever else makes sense for your use.

#include <random> // for mt19937, uniform_int_distribution
#include <iostream>

std::mt19937 seeded_engine() {
    std::random_device r;
    std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
    return std::mt19937(seed);
}

struct C {
    // Hold RNG state as a member variable
    std::mt19937 eng = seeded_engine();
    
    int foo() {
        // use the member variable to generate random numbers in a member function.
        return std::uniform_int_distribution<>(1,10)(eng);
    }
};

int main() {
    C c, d;
    std::cout << c.foo() << '\n';
    std::cout << d.foo() << '\n';
}

See this answer for an explanation of the implementation of seeded_engine().

(The above uses a few C++11 features besides <random>; uniform initialization, and in-class initialization for non-static members.)

like image 36
bames53 Avatar answered Sep 23 '22 21:09

bames53