在c++里怎么获得一组随即数?

来源:百度知道 编辑:UC知道 时间:2024/05/28 20:17:11

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
srand((unsigned)time(NULL));//随机种子,如果没有此句,多次运行程序,所获得的随机数是一样的,LZ可以删除此句试试
printf("%d\n", rand() % 100); //rand()是获得一个随机数,%100是指定随机数的范围,最后获得的是一百以内的随机数
scanf("%d",&i);
return 0;
}

#include<iostream>
#include <time.h>
int main()
{
int a[10];
srand((unsigned)time(NULL));//初始化随机函数
for (int i=0; i<10; i++)
{
a[i]=rand()%10;//生成小于10的整数
}
for (int j=0; j<10;j++)
{
cout<<a[j]<<ends;
}
return 0;
}