dimanche 19 novembre 2017

Normal distribution number producing strange values

I am following the example from:

http://ift.tt/10E902l

and seem to have run into this strange problem, I have followed the example as it seems to be almost the same, apart from from using some const variables.

The loop is meant to iterate N amount of times and produce a random number with a normal distribution. Then if the number is within a certain range it will increment that index in the array to specify the occurrence of that number.

I am however, getting very strange numbers, in the first iteration, it is saying that 4 has been counted 31 times but it is only the first iteration of the loop.

Am I missing something here or leaking into other parts of memory, any advice would be appreciated.

Here is the code

// first we need a number generator engine
std::default_random_engine randomEngine;

// then a distributor class
const double MEAN = 5.0;
const double STD_DEV = 3.0;
std::normal_distribution <double> distribution(MEAN, STD_DEV);

// keep on experimenting - eg - rolling the number generator n times to get
// a certain amount of values
const int MAX_TRIALS = 5;
// const int MAX_VALUES = 10;

// output some test values into an array and count
const int PLOT_MIN = 0;
const int PLOT_MAX = 10;
int plot[PLOT_MAX];

// for debug using 5
for (int i = 0; i < 5; i++) {

    // create a random number with the normal distribution transformation
    // pass the engine to the distributor
    int number = distribution(randomEngine);
    std::cout << "iteration: " << i << '\n';        
    // keep numbers within a range and count 


    // preincrement counts the number of times that number occure
    if (number >= PLOT_MIN && number < PLOT_MAX) {
        ++plot[number];            
        std::cout << "number " << number << '\n';
        std::cout << "count for " << number << " is: " << plot[number] << std::endl;
    }

}

Here are the strange results:

iteration: 0
number 4
count for 4 is: 31
iteration: 1
number 1
count for 1 is: 21853
iteration: 2
number 7
count for 7 is: 1
iteration: 3
number 1
count for 1 is: 21854
iteration: 4
number 5
count for 5 is: 1




Aucun commentaire:

Enregistrer un commentaire