所以我试图创建这个程序,从0到2随机生成一个10 x 10的随机整数矩阵.问题是程序总是给出相同的数字集.这是代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,它总是给我这个:
2211210012
2210121200
0020110222
2020012110
2020020121
2210012011
1220011211
2020120002
0201021221
0021212002
出于某种原因,它在多次运行程序后继续给我相同的输出.我希望得到一些帮助,我有预感,当你在你的机器上运行它会给你一个不同的矩阵所以请尝试多次运行它以查看连续运行的结果是否相同.我在这里先向您的帮助表示感谢!
use*_*587 10
srand()使用为程序的每个不同运行而更改的参数调用(通常time(0)可以正常工作).这将初始化伪随机数生成器的内部状态(正如您所注意到的,每当程序运行时,它都会设置为默认状态).
| 归档时间: |
|
| 查看次数: |
200 次 |
| 最近记录: |