Link to home
Start Free TrialLog in
Avatar of Elena Quinn
Elena Quinn

asked on

Random number generation (what's missing?)

I have tried a simple example straight off the Microsoft website Link in order to try to figure this out, but it won't compile for me.  I just want to create a uniformly distributed random number (long long).   I think I may be missing something, but I have no idea what.  

#include <random>
#include <iostream>

using namespace std;
using namespacestd::tr1;
//typedef std::tr1::random_device rd;

int main()
{
    random_device rd;   // non-deterministic generator
    mt19937 gen(rd());  // to seed mersenne twister.
    uniform_int_distribution<> dist(1,6); // distribute results between 1 and 6 inclusive.

    for (int i = 0; i < 5; ++i) {
        cout << dist(gen) << " "; // pass the generator to the distribution.
    }
    cout << endl;
}

Open in new window


Here are the errors I am getting.
User generated image
I would appreciate any insight.  I am using VS2008 w/SP1.

Thanks,
Elena
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Elena Quinn
Elena Quinn

ASKER

phoffric,

Does that mean it doesn't work in 2008 with SP1?  When initially researching this on the Microsoft site, the <random> included 2008.  But then I read on another question somewhere that I had to install SP1.  So I did that.

I kinda need it to work with 2008, unfortunately.  The developers I am working with are only using that.  Tried to get them to switch to 2013, but no go.

Thanks,
Elena
BTW, I did try rand() with not much success.  The distribution is not uniform at all.  See the pic of a distribution of 1000 tries.  User generated image
In what way did you try rand(), and how did results between 1 and 6 inclusive produce that histogram?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial