Using Mersenne Twister Random Number Generator

Hi, I am new and am trying to use the Mersenne Twister Random number generator to generate a random number between 0 and 1 in one spot, and an integer between 0 and a variable to be allocated at the beginning of each run-through of a program. I tried to find the syntax and such, but examples given did not work on my computer. I am currently using Code::Blocks as my ide/compiler. Could someone explain to me how to use this generator? I know i need to use the seed member function to seed the generator and the operator() member function to get the random number, but past that I can't get these to work. Here is a sample of code I am trying. I get some odd warning about this not being supported and the chrono and mt19937 classes don't appear to work:

// mersenne_twister_engine::operator()
#include <iostream>
#include <chrono>
#include <random>

int main ()
{
// obtain a seed from the system clock:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

std::mt19937 generator (seed); // mt19937 is a standard //mersenne_twister_engine
std::cout << "Random value: " << generator() << std::endl;

return 0;
}

Sounds like your compiler isn't recent enough to support this part of C++ 11.
closed account (o3hC5Di1)
Hi there,

I'm suspecting you are getting compiler / linker errors - could you copy paste them here too please?
You may need to specify to your compiler to use the C++11 / C++0x standard, as these headers you're including only became available since that standard.

Unfortunately I'm not exactly sure how to do this in Code::blocks - presumably there's a setiing for compilerflags somewhere, where you could specify:

 -std=c++0x


Presuming you're using Gcc/MingW, which I believe is the default compiler with Code::Blocks.

Hope that helps.
All the best,
NwN
omarkb93 wrote:
warning about this not being supported

As C++11 features are being developed right now by the compiler developers, there are some features which are not, or just partly supported at the moment.

What compiler are you using?
Last edited on
Topic archived. No new replies allowed.