C++ TR1: how to use the normal_distribution?

c++, normal-distribution, tr1

Solution

This definitely would not hang the program. But, not sure if it really meets your needs.

 #include <random>
 #include <iostream>

 using namespace std;

 typedef std::tr1::ranlux64_base_01 Myeng; 

 typedef std::tr1::normal_distribution<double> Mydist; 

 int main() 
 { 
      Myeng eng; 
      eng.seed(1000);
      Mydist dist(1,10); 

      dist.reset(); // discard any cached values 
      for (int i = 0; i < 10; i++)
      {
           std::cout << "a random value == " << (int)dist(eng) << std::endl; 
      }

 return (0); 
 }

Problem

I'm trying to use the C++ STD TechnicalReport1 extensions to generate numbers following a normal distribution, but this code (adapted from this article): ``` mt19937 eng; eng.seed(SEED); normal_distribution<double> dist; // XXX if I use the one below it exits the for loop // uniform_int<int> dist(1, 52); for (unsigned int i = 0; i < 1000; ++i) { cout << "Generating " << i << "-th value" << endl; cout << dist(eng) << endl; } ``` only prints 1 "Generating..." log message, then never exits the for loop! If I use the distribution I commented out instead, it terminates, so I'm wondering what I'm doing wrong. Any idea? Thanks a lot!

Original source