gpt4 book ai didi

C++ 快速随机数生成器

转载 作者:太空狗 更新时间:2023-10-29 23:36:01 28 4
gpt4 key购买 nike

<分区>

如果这是一个愚蠢的问题,我很抱歉,但我是 c++ 的新手,老实说找不到答案;

当我使用 rand() 时,当然我得先用srand()。

起初我只是导入 <ctime>并做 srand(time()) ,这奏效了。但如果我调用 rand()每秒不止一次 - 多久一次 time()更改 - 然后我会得到相同的答案。例如;

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
bool x = true;
while(x == true)
{
int num = 1;
srand(time(NULL));
num = rand();
cout<<num%10<<endl;
}

}

可能会产生类似于 666666666677777777700000000003333333333

这对我的目的没有好处 - 我更喜欢像 163509284749301935766 这样的东西。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com