C++ random uniform_int_distribution Class
Example
Roll a dice: get a random number from 1 to 6:
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dice(1, 6);
cout << dice(gen);
Definition and Usage
A uniform_int_distribution gives random whole numbers between a minimum and a maximum value. Both values are included, and every number has the same chance.
You give it an engine, like gen, every time you want a new number.
The uniform_int_distribution class is defined in the <random> header file.
Note: The output is random, so it is different each time you run the program.
Syntax
uniform_int_distribution<type> name(min, max);
Parameter Values
| Parameter | Description |
|---|---|
| type | Optional. A whole number type, like int (default) or long. |
| min | Required. The smallest possible number. |
| max | Required. The largest possible number. |
Technical Details
| Returns: | Calling name(engine) returns a random number from min to max. |
|---|