我正在 CentOS 5.2 中使用 Linux random() 函数生成随机消息。我想在 3 次随机调用后重置种子。换句话说,我希望第一次调用和第四次调用具有相同的输出。有什么方法可以将 rand() 函数设置为初始状态吗?或者你知道我可以做的任何其他功能吗?
最佳答案
您只需记住种子,然后用它来重置即可。 C 中类似这样的内容:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int seed = time(NULL);
int i;
for (i = 0; i < 10; i++) {
if (!(i % 3)) {
srandom(seed);
}
printf("%d\n", random());
}
}
关于c - Linux随机函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14838207/