我正在编写一款彩票应用程序。我有一个名为generateLotteryNumbers
的函数,它接受一个数组并用5个随机数填充该数组。我想要做的是每次调用此函数时生成不同的随机数集。
void generateLotteryNumbers(int lotteryNumbers[])
{
srand (time(NULL));
const int arraySize = 5;
int index = 0;
while (index < arraySize)
{
lotteryNumbers[index] = rand() % 50 + 1;
cout << lotteryNumbers[index] << endl;
index++;
}
}
目前的输出如下所示:
5
24
45
26
47
重复两次。
random_device
接口,可用于使用硬件熵源对生成器进行种子初始化。请参考 http://en.cppreference.com/w/cpp/numeric/random。 - zakinster