Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- seed_type prng_seed = 0; // suitable integral type
- MyRNG prng; // something from <random>
- void initPRNG()
- {
- bool seedsuccess = false;
- #ifdef WIN32
- HCRYPTPROV hCryptProv;
- BYTE* pbData = reinterpret_cast<BYTE*>(&prng_seed);
- if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
- {
- if (CryptGenRandom(hCryptProv, sizeof(prng_seed), pbData))
- {
- seedsuccess = true;
- }
- CryptReleaseContext(hCryptProv, 0);
- }
- #else
- std::ifstream urandom("/dev/urandom");
- if (urandom)
- {
- urandom.read(reinterpret_cast<char*>(&prng_seed), sizeof(prng_seed));
- seedsuccess = true;
- }
- #endif
- if (!seedsuccess)
- {
- prng_seed = std::time(NULL);
- }
- //std::cout << "Seeding the PRNG with: 0x" << std::hex << std::uppercase << std::setfill('0')
- // << std::setw(2 * sizeof(MyRNG::result_type)) << prng_seed << std::endl;
- prng.seed(prng_seed);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement