C++ random normal_distribution Class
Example
Get a random height, most often close to 170:
random_device rd;
mt19937 gen(rd());
normal_distribution<double> height(170.0, 10.0);
cout << height(gen);
Definition and Usage
A normal_distribution gives random decimal numbers that are most often close to the average (the mean). Numbers far from the average are rare.
The standard deviation says how spread out the numbers are. In the example, most heights are between 160 and 180.
The normal_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
normal_distribution<type> name(mean, stddev);
Parameter Values
| Parameter | Description |
|---|---|
| type | Optional. A decimal type, like double (default) or float. |
| mean | Optional. The average value. Default is 0.0. |
| stddev | Optional. The standard deviation (how spread out the numbers are). Default is 1.0. |
Technical Details
| Returns: | Calling name(engine) returns a random decimal number. |
|---|