Understanding random seed

Hey everyone.

Lets say I have a class which has multiple member functions which utilizes rand() % x + 1

Where would I put the seed? Do I initialize it in in the constructor so it will affect all member functions using rand()?

I want to achieve the most purest "possible" random number each time, but I am just confused as to where my seed should be placed, if rand()% is being utilized in various functions of the same class.
I want to achieve the most purest "possible" random number each time

Then you should immediately abandon rand() entirely, not to mention taking the result modulo n, which introduces bias.

The facilities in the header <random> are a step in the right direction, but all those generators are psuedorandom. If you need a true [CS]RNG or, use your operating system's facilities directly - there may be a hardware RNG.

Regarding rand:
Do I initialize it in in the constructor so it will affect all member functions using rand()?

No - the state of rand() is global, so seed it globally, when the program starts.
Last edited on
Unless you want to later repeat the sequence of random numbers it's best to only call srand() once, before the first call to rand(). At the beginning of main() is a good place to do it.
Topic archived. No new replies allowed.