gpt4 book ai didi

c++ - 随机数函数未正确随机化 (C++)

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:05:15 24 4
gpt4 key购买 nike

<分区>

所以我试图创建这个程序,它随机生成一个 10 x 10 的随机整数矩阵,从 0 到 2。问题是程序总是给出相同的数字集。这是代码:

#include <iostream>
#include <stdlib.h>

using namespace std;

int mapDraw(int array[], int mapMax, int mapBound){
for(int i = 0; i <= (mapMax - 1); i++){
if((i % mapBound)==0){
cout << endl;
}
cout << array[i];
}
return 0;
}

int main(){
int mapx = 10;
int mapz = mapx;
int mapArea = mapx * mapz;
int store[mapArea];
for(int i = 0; i <= (mapArea -1); i++){
int height = rand() % 3;
store[i] = height;
}
mapDraw(store, mapArea, mapx);
return 0;
}

当我运行代码时,它总是给我这个:

2211210012

2210121200

0020110222

2020012110

2020020121

2210012011

1220011211

2020120002

0201021221

0021212002

出于某种原因,它在多次运行该程序后继续给我相同的输出。我希望得到一些帮助,我有一种预感,当你在你的机器上运行它时,它会给你一个不同的矩阵,所以请尝试多次运行它,看看连续运行的结果是否相同。提前感谢您的帮助!

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