gpt4 book ai didi

c++ - 相同的随机数

转载 作者:行者123 更新时间:2023-11-30 02:58:57 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Same random numbers every loop iteration

这是我的代码:

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

int attack() {
srand((unsigned)time(NULL));
int hits=0;
for (int i=0;i<=3;i++) {
int a = rand() % 6;
if (a==6 || a==5)
{
hits++;
}
}
return hits;
}

int main () {
int a=attack();
cout << a << endl;
int b=attack();
cout << b << endl;
int c=attack();
cout << c << endl;
int d=attack();
cout << d << endl;
int e=attack();
cout << e << endl;
int f=attack();
cout << f << endl;
int g=attack();
cout << g << endl;
int h=attack();
cout << h << endl;
}

为什么我在 main() 中的所有变量都是相同的数字?

我只得到这样的输出:

1
1
1
1
1
1
1
1

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