Random numer generation question

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include<iostream>
#include<random>
#include<ctime>
#include<iomanip>
using namespace std;

int main()
{
default_random_engine numGenerator(static_cast<unsigned int>(time(NULL));
	uniform_int_distribution<unsigned int> range(1, 6);

	for (int i = 0; i <= 10; ++i)
	{
		cout << setw(10) << range(numGenerator);

		if(i % 5 == 0)
		{
			cout << endl;
		}
	}


	system("pause");
}


Hey guys, I'm trying to understand how this works, so if you see some misunderstanding in my explanation please correct me.

default_random_engine is a class like string, and saying default_random_engine numGenerator;is the same thing as saying int y, or float x? numGenerator is the instance of default_random_engine. If I was to write

default_random_engine numGenerator(static_cast<unsigned int>(time(NULL))

the "static_cast<unsigned int>(time(NULL)" portion is what is being passed to the constructor of numGenerator? I need to use a cast here because time is not an int value?

uniform_int_distribution is also its own class or type and range is the name of the instance, 1,6 are arguments passed to the constructor to determine range.

So by writing uniform_int_distribution<unsigned int> range(1, 6); I have created an object of uniform_int_distribution called range?

What I don't understand is if I write cout<< range(numGenerator)what is going on? Does range how two constructors? Anyone able to explain this to me?

Regards,

default_random_engine ... time is not an int value?

yes, throughout
uniform_int_distribution is also its own class or type

notice it is a class template, hence the instantiation with type parameter unsigned int in your code the type parameter could be any of short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long : http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
So by writing uniform_int_distribution<unsigned int> range(1, 6); I have created an object of uniform_int_distribution called range

yes which is, surprisingly enough, an uniform int distribution uniformly distributed on the closed interval [1, 6]
range(numGenerator)what is going on

here you have an overload of operator () defined for the range object that takes an argument numGenerator aka the generator object which, in turn,
supplies uniformly-distributed random integers through its operator() member function
(corresponding to your (time(NULL)))
The uniform_int_distribution object transforms the values obtained this way so that successive calls to this member function with the same arguments produce values that follow a uniform distribution within the appropriate range

http://www.cplusplus.com/reference/random/uniform_int_distribution/
So then the final question remains, what sort of random numbers are generated by the default_random_engine itself that are then scaled by the distribution object to give 'nice' random numbers b/w 1 and 6? Here, I'm afraid there's a bit of hand-waving on the reference websites as far as I can see, for e.g cplusplus says about std::default_random_engine:
It is the library implementation's selection of a generator that provides at least acceptable engine behavior for relatively casual, inexpert, and/or lightweight use
and cppreference does not say much more: http://en.cppreference.com/w/cpp/numeric/random
but if you are still interested you can try and dig up the actual C++ standard docs and see what else they have to say on this
Thanks alot!

great response.
Topic archived. No new replies allowed.