dimanche 31 janvier 2021

C++ Primer 5th Edition Random numbers [closed]

I am at chapter 17 generating Random numbers. C++ primer 5th ed.

I am stuck at this:

Generating Numbers That Are Not Uniformly Distributed In addition to correctly generating numbers in a specified range, another advantage of the new library is that we can obtain numbers that are nonuniformly distributed. Indeed, the library defines 20 distribution types! These types are listed in § A.3 (p. 882). As an example, we’ll generate a series of normally distributed values and plot the resulting distribution. Because normal_distribution generates floating-point numbers, our program will use the lround function from the cmath header to round each result to its nearest integer. We’ll generate 200 numbers centered around a mean of 4 with a standard deviation of 1.5. Because we’re using a normal distribution, we can expect all but about 1 percent of the generated numbers to be in the range from 0 to 8, inclusive. Our program will count how many values appear that map to the integers in this range:

int main(){

    default_random_engine e;        // generates random integers
    normal_distribution<> n(4,1.5); // mean 4, standard deviation 1.5
    vector<unsigned> vals(9);       // nine elements each 0

    for (size_t i = 0; i != 200; ++i) {
        unsigned v = lround(n(e));  // round to the nearest integer
        if (v < vals.size())        // if this result is in range
            ++vals[v];              // count how often each number appears
    }

    for (size_t j = 0; j != vals.size(); ++j)
        cout << j << ": " << string(vals[j], '*') << endl;
}

The output:

0: ***
1: ********
2: ********************
3: **************************************
4: **********************************************************
5: ******************************************
6: ***********************
7: *******
8: *

It works fine but I don't know what is a "mean of 4" and "standard deviation of 1.5".

"Because we’re using a normal distribution, we can expect all but about 1 percent of the generated numbers to be in the range from 0 to 8, inclusive."

  • When I googled I've seen something I couldn't understand the Gauss rule on Normal Distribution.

  • I know in statistics what "mean".

  • Please help.




Aucun commentaire:

Enregistrer un commentaire