example2_use_with_distributions.cpp

 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
34
35
36
37
38
39
40
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>

int main() {
    std::random_device rd;  // Used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()

    // Uniform distribution for integers between 1 and 100
    std::uniform_int_distribution<> uniform_dist(1, 100);

    // Normal distribution with mean 50 and standard deviation 10
    std::normal_distribution<> normal_dist(50.0, 10.0);

    std::vector<int> uniform_numbers;
    std::vector<double> normal_numbers;

    // Generate numbers
    for (int i = 0; i < 10; ++i) {
        uniform_numbers.push_back(uniform_dist(gen));
        normal_numbers.push_back(normal_dist(gen));
    }

    // Print uniform distribution results
    std::cout << "Uniform distribution (1-100): ";
    for (int num : uniform_numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Print normal distribution results
    std::cout << "Normal distribution (mean=50, std_dev=10): ";
    for (double num : normal_numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}
Back to std_mt19937