4

Besides being a rubbish programmer, my jargon is not up to scratch. I am going to try my best to explain myself. I have implemented a Merssene twister random number generator using randomlib. Admittedly I am not too familiar on how Visual 8 C++'s random number generator works, but I find I can seed it once srand(time(NULL)) in main() and I can safely use rand() in my other classes. The Merssene twister that I have one needs to create an object, and then seed that object.

#include <RandomLib/Random.hpp>
RandomLib::Random r;        // create random number object
r.Reseed();                 // seed with a "unique" seed
float d = r.FloatN();   // a random in [0,1] rounded to the nearest double

If I want to generate a random number in a class how do I do this without having to define an object each time. I am just worried that if I use the computer clock I will use the same seed each run (only changes every second).

Am I explaining myself right?

Thanks in advance

6
  • Looking at the documentation, you should be good with the Reseed() you have. randomlib.sourceforge.net/html/…
    – Mr. Llama
    Commented Oct 31, 2012 at 19:28
  • Why do you emphasize "correctly"? Did you assume we'd give an incorrect answer if you didn't say that?
    – Kerrek SB
    Commented Oct 31, 2012 at 19:32
  • Store the object as a static member in the class. That way, all objects of that class will use the same RandomLib::Random object.
    – Nikos C.
    Commented Oct 31, 2012 at 19:38
  • @GigaWatt I was looking at Reseed(), but it didn't feel right. Are you suggesting each time I generate a random numeber I use the code above? @KerrekSB Im not actually sure what I meant by correctly...
    – Seb
    Commented Oct 31, 2012 at 19:40
  • @NikosChantziaras I was playing around with that earlier. But I was still scared that by each unique class would be seeded by the same number if time(0) is used (i do know there are other seeding options)
    – Seb
    Commented Oct 31, 2012 at 19:42

2 Answers 2

2

The Random object is essentially state information that you need to preserve. You can use all the normal techniques: You could have it as a global variable or pass it around as a parameter. If a particular class needs random numbers you can keep a Random object as a class member to provide randomness for that class.


The C++ <random> library is similar in that it requires the construction of an object as the source of randomness/RNG state. This is a good design because it allows the program to control access to the state and, for example, guarantee good behavior with multiple threads. The C++ <random> library even includes mersenne twister algorithm.

Here's an example showing saving a RNG state as a class member (using std::mt19937 instead of Random)

#include <random> // for mt19937
#include <algorithm> // for std::shuffle
#include <vector>

struct Deck {
    std::vector<Cards> m_cards;
    std::mt19937 eng; // save RNG state as class member so we don't have to keep creating one

    void shuffle() {
        std::shuffle(std::begin(m_cards), std::end(m_cards), eng);
    }
};

int main() {
    Deck d;
    d.shuffle();
    d.shuffle(); // this reuses the RNG state as it was at the end of the first shuffle, no reseeding
}
6
  • How do I store it as a global variable (sorry for being naive). "If a particular class needs..." are you asking if its just one class?
    – Seb
    Commented Oct 31, 2012 at 19:28
  • 1
    Global variables are just variables defined at the global level; not in a function or a class or anything.
    – bames53
    Commented Oct 31, 2012 at 19:40
  • Is mt19937 part of visual 08?
    – Seb
    Commented Oct 31, 2012 at 19:49
  • I believe it was first added to Visual Studio in the 2010 version.
    – bames53
    Commented Oct 31, 2012 at 19:59
  • 1
    @user1002744 : It's in VC++ 2008 SP1, in the std::tr1 namespace. It is not in VC++ 2008 RTM nor in VC++ 2005 (I don't know what you mean by 'visual 8').
    – ildjarn
    Commented Oct 31, 2012 at 20:03
1

The accepted answer does not actually seed its mt19937, see this Q&A for a more thorough and complete answer on how this might be achieved and why there is no single solution:

How to succinctly, portably, and thoroughly seed the mt19937 PRNG?

TL;DR:

The question is relating to RandomLib but I will answer by referring to the STL implementations due to <random> being more accessible 10 years on. The principles should apply to all mt19937 implementations however.

std::mt19937 and std::mt19937_64 have an internal default seed which provides some state for the engine to work off. The default seeds will cause the engine to produce the same values every time unless re-seeded.

std::mt19937 provides two methods to seed it, both via the seed() function.

The first overload accepts a param of result_type (uint32_t for std::mt19937 and uint64_t for std::mt19937_64). Internally (at least in the MSVC implementation) this function will use the provided seed value to fill its internal state through a series of bit fiddling ops. Most quick-and-dirty examples will use a std::random_device to provide this seed value, but due to the standard allowing random_device to be just another PRNG it cannot be relied on in all circumstances, apparently this is (or was) the case with the MinGW compiler on Windows.

The second overload accepts a more generic generator/range param which can be used with std::seed_seq. The linked question has an example of how to create one of these.

Creating a seed_seq or a sufficiently random initial seed is a challenge and why the linked question is provided.

It is not recommended that you create a new Mersenne Twister PRNG every time you need one due to the seeding process being non-trivial. Instead, it is better to declare one once and hold onto it, either as a static, thread_local, global, or member of a class with a long lifetime.

1
  • 1
    While true, this doesn't answer the question. You can flag it as a duplicate of that one, and/or leave a comment. Commented Mar 27, 2022 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.