dimanche 16 octobre 2016

Types of random number generators and functions that take them as arguments

Following Stephan T. Lavavej's talk (see here), I am using a Mersenne Twister random number generator and am producing random numbers with this kind of code

#include <iostream>
#include <random>
int main()
{
  std::mt19937 mt(132);
  std::uniform_int_distribution<int> dist(0,50);
  for (int i =0;i<10;i++)
  {
     std::cout << dist(mt) << std::endl;
  }
}

I would like to shuffle using the shuffle function (and not the random_shuffle) function (as recommended in the talk again). From cppreference.com, I see the function takes a URBG&& as argument.

I don't really understand what a URBG is. I tried to feed the mt19937 instead and it seems to work fine.

#include <iostream>
#include <random>
int main()
{
  std::vector<int> v;
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);

  std::mt19937 mt(132);

  std::shuffle(v.begin(),v.end(),mt);
  for (int i=0;i<v.size();i++)
  {
    std::cout << v[i] << std::endl;
  }
}

Question

  • What is a URBG? Is mt19937 a subclass of URBG?
  • Can all types of random number generators given as argument to any function the produce a stochastic result (such as rand, shuffle or any function from random.h)?



Aucun commentaire:

Enregistrer un commentaire