example2_custom_range.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
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <iomanip>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    
    // Create a uniform real distribution between -10.0 and 10.0
    std::uniform_real_distribution<> dis(-10.0, 10.0);

    std::vector<double> numbers;

    // Generate 10 random numbers
    for (int i = 0; i < 10; ++i) {
        numbers.push_back(dis(gen));
    }

    // Sort the numbers
    std::sort(numbers.begin(), numbers.end());

    // Print the generated numbers
    std::cout << std::fixed << std::setprecision(4);
    std::cout << "Generated numbers in range [-10.0, 10.0):" << std::endl;
    for (double num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}
Back to std_uniform_real_distribution