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.
Compile errors
I would appreciate any insight.  I am using VS2008 w/SP1.

Thanks,
Elena
LVL 1
Elena QuinnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
If you download a later version of VS, the program will run as a C++11 program.
Just remove the line:
using namespacestd::tr1;

Here's where you can download VS Express:
https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Elena QuinnAuthor Commented:
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
Elena QuinnAuthor Commented:
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.  Distribution using rand()
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ozoCommented:
In what way did you try rand(), and how did results between 1 and 6 inclusive produce that histogram?
phoffric\Commented:
I don't have VS 2008, but I looked up items in MSDN. <random> is defined in VS 2008, but your error indicates that uniform_int_distribution is an undeclared identifier. This identifier in <random> was included in C++11:
http://www.cplusplus.com/reference/random/uniform_int_distribution/?kw=uniform_int_distribution

uniform_int_distribution is defined here in VS 2015:
https://msdn.microsoft.com/en-us/library/bb982398(v=vs.140).aspx
and here in VS 2013:
https://msdn.microsoft.com/en-us/library/bb982398(v=vs.120).aspx
but not in VS 2010;
and not here in VS 2008:
https://msdn.microsoft.com/en-us/library/bb982398(v=vs.90).aspx
phoffric\Commented:
Prior to C++11, there was TR1 which I now read can be included in VS 2008 by installing a Feature Pack. I don't believe that SP1 and Feature Pack are the same things. Let me know whether you downloaded the Feature Pack from here:
http://www.microsoft.com/en-us/download/details.aspx?id=6922

Possibly give that a try. I am not sure whether you will have problems in VS 2008 getting the correct <random>, but one step at a time. Possibly the Feature Pack installation overwrites the <random>.
phoffric\Commented:
If you already did install the Feature Pack and it doesn't work, then you can install boost libraries from here:
http://www.boost.org/users/download/

As you can see from this library list, Random is included in boost, and it says: Standard ... TR1.
http://www.boost.org/doc/libs/

The uniform_int_distribution class header file is defined here:
http://www.boost.org/doc/libs/1_59_0/boost/random/uniform_int_distribution.hpp

You wrote that you read that you had to install SP1. Possibly that was just to get the <random> feature, but appears not to include the uniform_int_distribution class.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.