利用系统流失时间做种子,产生随机数
#include
#include
using namespace std;
int main()
{
srand(time(0));
int result[10];
for(int i = 0; i < 10; i++)
{
result[i] = rand() % 100;
cout << result[i] << " ";
}
return 0;
}
产生伪随机数:
#include
#include
using namespace std;
int main()
{
srand(100);
int result[10];
for(int i = 0; i < 10; i++)
{
result[i] = rand() % 100;
cout << result[i] << " ";
}
return 0;
}
#include
#include
#include
int main()
{
srand((unsigned)time(0));
int Array[10];
int i=0;
double sum=0;
int Max=-1;
int Min=100;
while(i<10)
{
Array[i]=rand()%51;
if(Array[i]<10)
continue;
if(Array[i]>Max)
Max=Array[i];
if(Array[i]
sum+=Array[i];
i++;
}
for(int j=0;j<10;j++)
printf("第 %d 个元素: %d\n",j+1,Array[j]);
printf("最大值: %d\n",Max);
printf("最小值: %d\n",Min);
printf("平均值: %lf\n",sum/10.0);
system("pause");
return 0;
}
以上是我写的代码 楼主可以自己试着写一下
#include
#include
#include
#define MAX 10
int main( void)
{
int number[MAX] = {0};
int i;
srand((unsigned) time(NULL)); /*播种子*/
for(i = 0; i < MAX; i++)
{
number[i] = rand() % 101; /*产生101以内的随机整数*/
while(number[i]==0)
number[i] = rand() % 100;
printf("%d ", number[i]);
}
printf("\n");
return 0;
}