gpt4 book ai didi

c++ - 单个输出显示相同的数字,但当有超过 1 个输出时,输出符合预期

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:34 25 4
gpt4 key购买 nike

<分区>

这是一个小程序:

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
long x = rand();
cout << x << endl;
}

它总是显示41。但是如果我像这样修改程序,

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
for( int i = 0 ; i <= 9 ; i++ ) {
long x = rand();
cout << x << endl;
}
}

输出结果和预期的一样,是随机数的集合。输出:

41

18467

6334

26500

19169

15724

11478

29358

26962

24464

但为什么我在运行第一个程序时得到相同的数字 rand 实际上是如何工作的?

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