lundi 30 novembre 2015

C++11 discarding numbers in a PRNG sequence

I am trying to use the function discard to skip numbers in a sequence of random numbers. Here is my try:

#include <random>
#include <iostream>

using namespace std;

int main ()
{
  unsigned seed = 1;
  uniform_real_distribution<> dis(0,10);
  mt19937 gen (seed);
  cout << dis(gen) << endl;
  //gen.discard(1); // supposed to be the same of `dis(gen)`?
  cout << dis(gen) << endl;
  cout << dis(gen) << endl;
}

The output of this code is

9.97185
9.32557
1.28124

If I uncomment the line with gen.discard(1) I get

9.97185
0.00114381
3.02333

but I was expecting that the first two numbers are 9.97185 and 1.28124, since the number 9.32557 would be skipped.

Q: How to use discard properly or, is there an alternative solution with the same effect I want? I could simply use dis(gen), but maybe it i

Thanks.




Aucun commentaire:

Enregistrer un commentaire