Return to Snippet

Revision: 5975
at April 18, 2008 06:00 by tkf


Initial Code
#include <iostream>
#include <ctime>
#include <boost/random.hpp>

class Random
{
public:
  boost::mt19937 gen;
  boost::uniform_int<int> dst;
  boost::variate_generator< boost::mt19937, boost::uniform_int<int> > rand;
  Random( int N ):// call instance:
    gen( static_cast<unsigned long>(std::time(0)) ), dst( 0, N ), rand( gen, dst ) {
  }
  std::ptrdiff_t operator()( std::ptrdiff_t arg ) {
    return static_cast< std::ptrdiff_t >( rand() );
  }
};

int main () {
  srand ( unsigned ( time (NULL) ) );
  std::vector<int> myvector;
  std::vector<int>::iterator it;
  Random rnd( 10 );

  // set some values:
  for (int i=0; i<10; ++i) myvector.push_back(i); // 0 1 2 3 4 5 6 7 8 9

  // using built-in random generator:
  std::random_shuffle ( myvector.begin(), myvector.end() );

  // using myrandom:
  std::random_shuffle ( myvector.begin(), myvector.end(), rnd);

  // print out content:
  std::cout << "myvector contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << " " << *it;

  std::cout << std::endl;
  return 0;
}

Initial URL


Initial Description
use boost random algolism in std::random_shuffle

Initial Title
std::random_shuffle and boost/random.hpp

Initial Tags


Initial Language
C++