7

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Alex
  • 671
  • 3
  • 11
  • 25
  • 3
    I would stay away from `rand` and use a proper random number class such as [these](http://en.cppreference.com/w/cpp/numeric/random). – Pubby Nov 04 '12 at 02:25
  • @pubby I suppose I could consider something like that in the future, but for now I'm trying to stick to standard libraries with this project. – Alex Nov 04 '12 at 02:27
  • 2
    Initializing your static variable to false is pretty trivial: `int YourRandClass::seeded = false;` (in one source file, not in the class definition). FWIW, the classes @pubby are suggesting *are* part of the C++11 standard library. – Jerry Coffin Nov 04 '12 at 02:27
  • 4
    @Alex that is part of the C++11 standard library. – David Brown Nov 04 '12 at 02:29
  • Oh, that's good to know, I'll have to read into that a bit. Scanning the link he gave me I thought it was an external library. – Alex Nov 04 '12 at 02:40

5 Answers5

6

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;
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • I thought I needed to do something like this, but I was having a hard time finding the exact syntax I needed to use. Thanks. – Alex Nov 04 '12 at 02:34
  • @Alex: Yeah, it's a little funky, not extremely intuitive, though it makes sense when you think about it. – Ed S. Nov 04 '12 at 02:38
5

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

bames53
  • 86,085
  • 15
  • 179
  • 244
  • I am definitely going to read some more about this. My training in C++ seems to be a bit outdated. – Alex Nov 04 '12 at 02:59
2

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;
}
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • Wouldn't I still need to make sure seeded was false before executing the seed() function? Does this cause the seeded variable to be set only a single time throughout all instances of the class? – Alex Nov 04 '12 at 02:39
  • 1
    @Alex: If you always returns true - then not, do not test seeded for true. Yes, it will be set only once - and this works well even in multithread enviroment. – PiotrNycz Nov 04 '12 at 02:41
  • 1
    @Alex The initializer for a static variable is executed only once, so you don't have to check anything. The compiler will execute `seed()` once and you don't need to worry about how that is accomplished. – bames53 Nov 04 '12 at 02:50
  • That is a very good trick to know about. I will try this out. – Alex Nov 04 '12 at 02:52
1

The problem is similar to that of singleton instantiation. Use a feature of your OS like pthread_once or boost::call_once and a static member to execute the seed one and only one time.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • I like the suggestion, but I am actually trying to stay as cross-platform with this program as I can. Using an OS-specific function would cause me to have to change the code for each OS. – Alex Nov 04 '12 at 02:44
  • 1
    Well, `boost::call_once` is cross platform. But you only need it or `pthread_once` if you're concerned about concurrency at initialization time. – Brian Cain Nov 04 '12 at 02:48
  • I suppose I should read a bit more about these functions. They could be a big help in the future. – Alex Nov 04 '12 at 02:54
0

bames53 had a great answer. Now to pull that all nicely into a standalone integer generating function:

int generateRandom(int min, int max) {
   std::mt19937 eng{std::chrono::high_resolution_clock::now().time_since_epoch().count()};
   return std::uniform_int_distribution<>(min,max)(eng);
}
Cameron Forward
  • 702
  • 8
  • 16