我正在尝试为伪随机数生成器生成一个好的随机种子。我想我会得到专家的意见。让我知道这是否是一种不好的方法,或者是否有更好的方法。
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
unsigned int good_seed()
{
unsigned int random_seed, random_seed_a, random_seed_b;
std::ifstream file ("/dev/random", std::ios::binary);
if (file.is_open())
{
char * memblock;
int size = sizeof(int);
memblock = new char [size];
file.read (memblock, size);
file.close();
random_seed_a = int(memblock);
delete[] memblock;
}// end if
else
{
random_seed_a = 0;
}
random_seed_b = std::time(0);
random_seed = random_seed_a xor random_seed_b;
return random_seed;
} // end good_seed()