源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

c语言生成随机数的方法(获得一组不同的随机数)

  • 时间:2020-02-08 13:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c语言生成随机数的方法(获得一组不同的随机数)
[u]复制代码[/u] 代码如下:
//以空间换时间 /* arr :数组名 n  :给定的数组长度 转移数组中的数据 */ void diffarr(int arr[],int n) {     int sn=n,index,*ptr=arr;     int *pfrom=new int[sn];     for(int i=0;i<sn;i++)         *(pfrom+i) =i+1 ;     srand(time(0));     for(int k=0;k<sn;)     {         index=rand()% sn;         if(*(pfrom+index)!=0)         {   //从源数据pfrom取,每取一个将其置为0,直至pfrom中所有元素都为0             *(ptr+k)=pfrom[index];             pfrom[index]=0;             k++;         }     }     delete [] pfrom; }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部