dimanche 21 janvier 2018

Can I use a single `default_random_engine` to create multiple normally distributed sets of numbers?

I want to generate a set of unit vectors (for any arbitrary dimension), which are evenly distributed across all directions. For this I generate normally distributed numbers for each vector component and scale the result by the inverse of the magnitude.

My question: Can I use a single std::default_random_engine to generate numbers for all components of my vector or does every component require its own engine?

Afaik, each component needs to be Gaussian-distributed independently for the math to work out and I cannot assess the difference between the two scenarios. Here's a MWE with a single RNG (allocation and normalization of vectors is omitted here).

std::vector<std::vector<double>> GenerateUnitVecs(size_t dimension, size_t count)
{
    std::vector<std::vector<double>> result;

    /* Set up a _single_ RNG */
    size_t seed = GetSeed(); // system_clock
    std::default_random_engine gen(seed);
    std::normal_distribution<double> distribution(0.0, 1.0); 

    /* Generate _multiple_ (independent?) distributions */
    for(size_t ii = 0; ii < count; ++ii){
        std::vector<double> vec;
        for(size_t comp = 0; comp < dimension; ++comp)
            vec.push_back(distribution(gen)); // <-- random number goes here

        result.push_back(vec);
    }
    return result;
}

Thank you.




Aucun commentaire:

Enregistrer un commentaire