summaryrefslogtreecommitdiffstats
blob: 8b1dee0756cc03da541e588e0b982fe9eda3e7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * Copyright (c) 2012-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Base/StdRandomGenerator.h>

#include <algorithm>
#include <functional>

namespace {
    template<class T = std::mt19937, std::size_t N = T::state_size>
    typename std::enable_if<!!N, T>::type createSeededRandomEngine() {
        typename T::result_type random_data[N];
        std::random_device source;
        std::generate(std::begin(random_data), std::end(random_data), std::ref(source));
        std::seed_seq seeds(std::begin(random_data), std::end(random_data));
        return T(seeds);
    }
}

namespace Swift {

StdRandomGenerator::StdRandomGenerator() : generator(createSeededRandomEngine()) {
}

int StdRandomGenerator::generateRandomInteger(int maximum) {
    std::uniform_int_distribution<> distribution(0, maximum);
    return distribution(generator);
}

}