mardi 27 juin 2023

Is there a way to create a 'truer' random outcome using a Gaussian normal distribution in C?

I've scoured stack overflow for some descriptions, explanations, and snippets of the std::normal_distribution<> variable(mean, stddev) function, and have found one instance in particular, listed below, that works quite effectively for my purpose, which is simply to be able to create and house a random number from a normal distribution given the mean and stddev.

#include <random>
...
std::mt19937 generator;
double mean = 100.0, stddev  = 15.0, example;
std::normal_distribution<double> normal(mean, stddev);
example = normal(generator);
cout << "Normal: " << example << endl;

credit - https://stackoverflow.com/a/11977979/14316685.

The one problem that I"ve run into, however, is that the results from this code become quite repetitive and predicatable over time. For example, I've repeatedly used a mean of 100 and a stddev of 15, which when run over 1000 instances almost assuredly, produces exactly one instance of both approximately 52.246 and 156.86 consistently, at least on my system.

Is there a way to manipulate this code snippet, or seed if I understand this correctly, so that it produces a variety of results that are different enough each time while still obeying the normal distribution?

As I'm still new to this function, I tried utilizing std::default_random_engine generator; in the place of std::mt19937 generator, and while it produced a different result, it had a similar problem of repetition over time.




Aucun commentaire:

Enregistrer un commentaire