Seeding rand() for a C++ class

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;
}

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


Use static variable feature that it is initialized only once:

static bool seed()
{
  srand(time(NULL));
  return true;
}
myConstructor(){
  static bool seeded = seed();
  myVariable = rand()%maxVal;
}