注册 登录
编程论坛 C++教室

[求助]能不能让数组中装满随机数?

a8451727 发布于 2007-07-22 16:32, 591 次点击
如题
请大家教下小弟

如:int a[5][5]=rand()%100; 我知道这样不对,但我意思是这样

还有为什么这样不行?
for(int i=0;i<25;i++)
{
int t=rand()%1000;
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
a[i][j]=t;
}
4 回复
#2
jianvsgao2007-07-22 16:50

#include<iostrem>
#incldue<ctime>
using namespace sd;

void get(){ //获得随机数
unsigned int seed=time(NULL);
unsigned int number=seed*37%100; //乘以100是获得100以内的随机数
}

int main()
{
int array[10];
for(int i=0;i<10;i++) //随机数放到数组
{
array[i]=get();
}

}

#3
jianvsgao2007-07-22 16:51
那个是 core c++
#4
a84517272007-07-22 20:59
谢谢   等下试下
#5
HJin2007-07-22 22:06
rand_max is 65536--- so that you will get lower (or higher, dependent on how you define these two terms) word of randomness.

A quick but still naive cure is:

int random(int a, int b)
{
return a+(rand() | (rand()<<16)) % (b-a+1);
}

You have to seed once in your program.
1