Created
March 9, 2023 20:14
-
-
Save bwedding/198eb099da5e69a6f1f707030385c032 to your computer and use it in GitHub Desktop.
Generate Random Numbers in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RandomGenerator.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
// | |
#include <iostream> | |
#include <random> | |
#include <array> | |
#include <cstdint> | |
class RandomNumbers | |
{ | |
std::random_device rd; | |
std::array<std::uint32_t, 8> seed; | |
std::seed_seq seq; | |
std::mt19937_64 mt; | |
std::uniform_int_distribution<std::uint64_t> dist; | |
std::array<std::uint32_t, 8> GenerateRandomSeed() | |
{ | |
for (auto& s : seed) | |
s = rd(); | |
std::mt19937_64 mt(seq); | |
std::random_device r; | |
return seed; | |
} | |
public: | |
RandomNumbers() : dist(0, ULONG_MAX) | |
{ | |
seed = GenerateRandomSeed(); | |
seq.generate(seed.begin(), seed.end()); | |
} | |
std::uint64_t GetRandomNumber() | |
{ | |
return dist(mt); | |
} | |
}; | |
int main() | |
{ | |
RandomNumbers rn; | |
for (int i = 0; i < 20; i++) | |
std::cout << "Random Number: " << rn.GetRandomNumber() << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment