- #include <iostream>
- #include <iomanip>
- #include <stdio.h>
- using namespace std;
- void print(int i_Array[],int count); //声明输出随机数的函数
- int chackArray(int i_Value,int i_Array[],int count); //声明检查产生的随机数是否存在函数
- void getArray(int i_Array[],int count); //获得随机数数组函数
- int main()
- {
- const int count = 10; //定义要输出的随机数的个数
- int i_Array[count] = {0}; //初始化输出随机数的数组
- getArray(i_Array,count); //获得随机数数组
- print(i_Array,count); //输出生成的随机数
- system("pause"); //暂停定屏
- return 0;
- }
- //定义输出数组的函数
- void print(int i_Array[],int count)
- {
- for(int i = 0; i < count;i++)
- {
- cout<<setw(5)<<i_Array[i];
- if(i%10 == 9)
- cout<<endl;
- }
- return;
- }
- //定义获得数组的函数,其中Number为数组中的第几个数字
- void getArray(int i_Array[],int count)
- {
- int Number = 0;
- for(int i =0;;i++)
- {
- int i_Value = rand()%100; //获得一随机数
- if(chackArray(i_Value,i_Array,Number)) //检查是否在数组里存在,如果不存在则执行下面的过程否则跳过下面的过程
- {
- i_Array[Number] = i_Value; //将获得的数字赋给数组中的第Number个数字
- Number++;
- if(Number == count ) //如果获的数字个数达到要求,则返回,反之集训
- return;
- }
- }
- return;
- }
- //定义检查重合的函数,其中i_Value为要检查的数,i_Array[]为检查的数组范围,count为数组中的数的个数,如果被检查数不存在则返回1,否则返回0
- int chackArray(int i_Value,int i_Array[],int count)
- {
- int result = 1;
- for(int i = 0; i<count;i++)
- {
- if(i_Value == i_Array[i])
- return 0;
- }
- return 1;
- }
- //该片段来自于http://www.codesnippet.cn/detail/230120131854.html
来源: http://www.codesnippet.cn/detail/230120131854.html