C++ Help Needed


How do you randomly select a uniform value from a range of numbers?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <random>
#include <chrono>
#include <vector>
#include <iostream>

int main() {
    unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();

    std::vector<int> v;
    for (int i = 0; i < 100; ++i)
        v.push_back(i);


    std::mt19937 randGen (seed);
    std::uniform_int_distribution<int> dist(0, v.size()-1);

    std::cout << v.at(dist(randGen)) << std::endl;

    return 0;
}


http://ideone.com/5E7Vz6

Topic archived. No new replies allowed.