您的位置:首页 > 其它

生成互不相同的随机数

2012-06-01 08:03 405 查看
方案《一》

//生成互不相同的随机数(效率较高)

//作者   feiyelove

//2008 12月4日

#include <iostream>

#include <ctime>

#include <cassert>

using namespace std;

int main()

{                  
void ran_num(int low,int hight,int n);
cout<<"请输入你所要取数的区间,以及所要取的随机数个数,中间以空格分隔,例如 0 100 10 以回车结束输入"<<endl;
int low,hight,n;
cin>>low>>hight>>n;
cout<<"以下为输出结果:"<<endl;
ran_num(low,hight,n);
return 0;

}

//在区间【low,high】之间取n个互不相同的随机数(包括low,hight)

void ran_num(int low,int hight,int n)

{
if(low>hight)
{
low=low^hight;
hight=low^hight;
low=low^hight;
}
srand((unsigned)time(NULL));                  //设置随机数产生种子
static int count;
if((hight-low+1)<n)
{
cout<<"所给区间["<<low<<","<<hight<<"]太小,不可能取出"<<n<<"个不同随机数"<<endl;
return;
}
int i,*num_random=new int
;                 //存放取出的随机数
bool *b_array=new bool[hight-low+1]; 
assert((num_random!=NULL) && (b_array!=NULL) );     
for(i=0;i<hight-low+1;i++)
b_array[i]=false;                         
for(i=0;i<n;i++)
{
int temp=0;
do
{
temp=(int)rand()%(hight-low+1)+low;     //生成随机数
count++;
}while(b_array[temp-low]);          
num_random[i]=temp;
b_array[temp-low]=true;
}                                                        //核心代码
for(i=0;i<n;i++)
cout<<num_random[i]<<endl;
cout<<"程序运行"<<count<<"次"<<endl;
delete [] num_random;
num_random=NULL;
delete [] b_array;
b_array=NULL;

}

/*

思路:先先初始化一个bool型的数组,将其值全赋为false,然后生成随机下标

并赋值此下标为true作为标志,以求下次取得不同的随机数

*/


方案《二》

//生成互不相同的随机数(效率最高)

//作者   feiyelove

//2008 12月5日

#include <iostream>

#include <ctime>

#include <cassert>

using namespace std;

int main()

{                  
void ran_num(int low,int hight,int n);
cout<<"请输入你所要取数的区间,以及所要取的随机数个数,中间以空格分隔,例如 0 100 10 以回车结束输入"<<endl;
int low,hight,n;
cin>>low>>hight>>n;
cout<<"以下为输出结果:"<<endl;
ran_num(low,hight,n);
return 0;

}

//在区间【low,high】之间取n个互不相同的随机数(包括low,hight)

void ran_num(int low,int hight,int n)

{
if(low>hight)
{
low=low^hight;
hight=low^hight;
low=low^hight;
}
srand((unsigned)time(NULL));                  //设置随机数产生种子
static int count;
if((hight-low+1)<n)
{
cout<<"所给区间["<<low<<","<<hight<<"]太小,不可能取出"<<n<<"个不同随机数"<<endl;
return;
}

int i,*num_random=new int
;                 //存放取出的随机数
int *i_array=new int[hight-low+1];
assert((num_random!=NULL) && (i_array!=NULL) );      
for(i=0;i<hight-low+1;i++)                    
i_array[i]=i+low;
for(i=0;i<n;i++)   
{   
int temp=(int)rand()%(hight-low+1-i);   
num_random[i]=i_array[temp];   
i_array[temp]=i_array[hight-low-i];
count++;
}                                                          //核心代码
for(i=0;i<n;i++)
cout<<num_random[i]<<endl;
cout<<"程序运行"<<count<<"次"<<endl;
delete [] n
4000
um_random;
num_random=NULL;
delete [] i_array;
i_array=NULL;

}

/*

思路:先先初始化一个int型的数组,将区间所有值赋予它,然后生成随机下标,

将i_array[hight-low-i]赋值于i_array[temp],逐步靠近i_array[0]

*/


方案《三》




//生成互不相同的随机数(效率最低)

//作者   feiyelove

//2008 12月5日

#include <iostream>

#include <ctime>

#include <cassert>

using namespace std;

int main()

{                  
void ran_num(int low,int hight,int n);
cout<<"请输入你所要取数的区间,以及所要取的随机数个数,中间以空格分隔,例如 0 100 10 以回车结束输入"<<endl;
int low,hight,n;
cin>>low>>hight>>n;
cout<<"以下为输出结果:"<<endl;
ran_num(low,hight,n);
return 0;

}

//在区间【low,high】之间取n个互不相同的随机数(包括low,hight)

void ran_num(int low,int hight,int n)

{
if(low>hight)
{
low=low^hight;
hight=low^hight;
low=low^hight;
}
srand((unsigned)time(NULL));                  //设置随机数产生种子
static int count;
if((hight-low+1)<n)
{
cout<<"所给区间["<<low<<","<<hight<<"]太小,不可能取出"<<n<<"个不同随机数"<<endl;
return;
}

int i,*num_random=new int
;                 //存放取出的随机数
assert(num_random != NULL);      
num_random[0] = (int)rand()%(hight-low+1)+low;
for (i=1; i<n; i++)
{
int temp = (int)rand()%(hight-low+1)+low;
for (int j=0; j<i; j++)                             //改为for (int j=0; j<i; )
{                                                   //    {
if (temp != num_random[j])                      //  if (temp != num_random[j]) 
{                                               //        {
num_random[i] = temp;                       //     num_random[i] = temp; 
//j++;                                      //            j++;
}                                               //        }
else                                            //        else
{                                               //        {
temp = (int)rand()%(hight-low+1)+low;       //     temp = (int)rand()%(hight-low+1)+low;
j = -1;                                     //            j=0;
}                                               //        }
count++;                                        //        count++;
}                                                   //     }
}                                             //核心代码
for(i=0;i<n;i++)
cout<<num_random[i]<<endl;
cout<<"程序运行"<<count<<"次"<<endl;
delete [] num_random;
num_random=NULL;

}

/*

思路:每次先取一个数,然后与以前所取出的每个数作比较,假如都不相等,

则存在num_random中

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  random null ini