MW

Tag maxwell

Recent Posts

Maxwell distribution in C++11 (July 29, 2014)

Hey all,

I recently needed to generate values following a Maxwell distribution. Wikipedia gives the hint that this is a Gamma distribution, which in C++11 is easily useable. Thus, thanks to <random>, we can setup a Maxwell distribution in a few lines of code:

    #include <random>
    #include <chrono>
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
        unsigned seed = chrono::system_clock::now().time_since_epoch().count();
        default_random_engine generator(seed);
     
        // Boltzmann factor times temperature
        const double k_T = 0.1;
     
        // setup the Maxwell distribution, i.e. gamma distribution with alpha = 3/2
        gamma_distribution<double> maxwell(3./2., k_T);
     
        // generate Maxwell-distributed values
        for (int i = 0; i < 10000; ++i) {
            cout << maxwell(generator) << endl;
        }
     
        return 0;
    }

Pretty neat, I have to say!

continue reading...