0

I need to implement a good RNG and I guess Mersenne Twister could be good for me. I didn't find anywhere a working C++ implementation, am I a bad google searcher or is it really not simple to find?! I've tried before rand():

srand((unsigned)time(0));
for (int i = 0; i < 9; i++) {
     random =  rand();
     cout<<random;
}

I don't know why, but the random variable is always the same number.....BUT if I add a Sleep(1000), it works! Like this:

srand((unsigned)time(0));
for (int i = 0; i < 9; i++) {
    random =  rand();
    cout<<random;
    Sleep(1000);
}

So I decided to try Mersenne Twister...Could someone find a solution of this (because I need to find a very very big number of random numbers so I can't use Sleep(1000), it would takes eras!) or help me to implement Mersenne Twister or maybe also another good RNG. Thanks and sorry for my bad English...

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Have a look at this: http://stackoverflow.com/q/9471604/1168156 – LihO Oct 07 '13 at 08:58
  • 1
    You are a bad googler, as there is a mersene twister engine in the [standard library](http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine) and in [boost](http://www.boost.org/doc/html/boost_random/reference.html) – KillianDS Oct 07 '13 at 08:58
  • 2
    http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – knivil Oct 07 '13 at 08:59
  • This is good question too: http://stackoverflow.com/q/4195958/1168156 – LihO Oct 07 '13 at 09:02
  • boost has one, and even c++ comes with a mersenne twister... so the question is, what is not "good" in them? – PlasmaHH Oct 07 '13 at 09:09
  • 2
    I think you really had the srand() call inside the loop too. there would normally be no reason adding Sleep() would affect the result unless srand() was called inside your loop as well. – nos Oct 07 '13 at 09:11

2 Answers2

7

C++11 includes a mersenne twister with the random standard library header.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
1

Boost.Random is an available implementation, if your STL implementation doesn't have it.

You can use random_device to get a random seed, and then pass it to the mt19937 constructor to initialize the random number generator.
Then you can use uniform_int_distribution to get a random integer, uniformly distributed in the closed range [a, b] that you specify in uniform_int_distribution constructor.

You may want to consider this sample code (compiled with g++ 4.8.1):

#include <iostream>
#include <boost\nondet_random.hpp>
#include <boost\random.hpp>

int main() {        
    // Get a random seed
    boost::random_device rd;
    const auto seed = rd();

    // Marsenne Twister generator
    boost::random::mt19937 mt(seed);

    // Get random integers uniformly distributed in range [0, 99]
    boost::random::uniform_int_distribution<int> dist(0, 99);

    // Print 10 random numbers, uniformly distributed in previous range
    for (int i = 0; i < 10; ++i) {
        std::cout << dist(mt) << " ";
    }
    std::cout << std::endl;    
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162