April 14, 2013

Pseudo-Random Numbers Generation with C++11

A short post on how to generate random variables with the new header "random" in the STL. I provide a header file that is useful to me whenever I use this new feature; maybe you'll find it useful too. Here is a usage example (assuming you put the header in the same directory):


#include "random_engine.h"
#include <iostream>

using namespace std;

int main()
{
      std::uniform_int_distribution<unsigned> U(1, 15);
      auto generator = RandomEngine::bind_to(U);

      cout << generator() << endl;
      cout << generator() << endl;
      cout << generator() << endl;
      cout << generator() << endl; 

      cout << "------------" << endl; 

      cout << U( *RandomEngine::get() ) << endl;
      cout << U( *RandomEngine::get() ) << endl;
      cout << U( *RandomEngine::get() ) << endl;
      cout << U( *RandomEngine::get() ) << endl;
}

No comments:

Post a Comment