Java产生不重复随机数方法
创始人
2024-04-01 14:31:05
0

关于生成Java不重复的随机数:

  1. import java.util.*;  
  2. public class Test...{  
  3.     public static void main(String[] args)...{  
  4.         //生成 [0-n) 个不重复的随机数  
  5.         / st 用来保存这些随机数  
  6.         ArrayList list = new ArrayList();  
  7.           
  8.           
  9.         int n = 10;  
  10.         Random rand = new Random();  
  11.         boolean[] bool = new boolean[n];  
  12.           
  13.         int num =0;  
  14.           
  15.         for (int i = 0; i              
  16.       
  17.             do...{  
  18.                 //如果产生的数相同继续循环  
  19.                 num = rand.nextInt(n);      
  20.                
  21.             }while(bool[num]);  
  22.               
  23.             bool[num] =true;  
  24.               
  25.             list.add(num);  
  26.           
  27.           
  28.         }  
  29.               
  30.       
  31.         System.out.println (list);      
  32. }          
  33.           
  34.  
  35.  
  36. public class Test  
  37. {  
  38.  
  39.     public static void main(String[] args)  
  40.     {  
  41.         int[] arr = new int[10];  
  42.  
  43.         for (int i = 0; i < 10; i++)  
  44.         {  
  45.             arr[i] = (int) (Math.random() * 40) + 1;  
  46.             for (int j = 0; j < i; j++)  
  47.             {  
  48.                 if (arr[j] == arr[i])  
  49.                 {  
  50.                     i--;  
  51.                     break;  
  52.                 }  
  53.             }  
  54.         }  
  55.         for (int i = 0; i < 10; i++)  
  56.             System.out.print(arr[i] + " ");  
  57.     }  
  58. }  
  59.  
  60.  
  61. b.  
  62.  
  63. Java code  
  64.  
  65.  
  66. import   java.util.*;   
  67. public   class   Test   
  68. {   
  69.           
  70.         public   static   void   main(String[]   args)   
  71.         {   
  72.                 int   n=40;   
  73.                 int[]   num   =   new   int[n];   
  74.                 for(int   i=0;i                         num[i]   =   i+1;   
  75.                 int[]   arr   =   new   int[10];   
  76.                 for(int   i=0;i                 {   
  77.                         int   r   =(int)(Math.random()*n);   
  78.                         arr[i]=num[r];   
  79.                         num[r]=num[n-1];   
  80.                         n--;   
  81.                 }   
  82.                 for(int   i=0;i                         System.out.print(arr[i]+"   ");   
  83.         }   
  84. }  
  85.  
  86.  
  87.  
  88. c.  
  89.  
  90. Java code  
  91.  
  92.  
  93. import   java.util.*;   
  94. public   class   Test   
  95. {   
  96.           
  97.         public   static   void   main(String[]   args)   
  98.         {   
  99.                 LinkedList    myList=   new   LinkedList  ();   
  100.                 int   n=40;   
  101.                 for(int   i=0;i                         myList.add(i+1);   
  102.                 int[]   arr   =   new   int[10];   
  103.                 for(int   i=0;i                 {   
  104.                         arr[i]=myList.remove((int)(Math.random()*n));   
  105.                         n--;   
  106.                 }   
  107.                 for(int   i=0;i                 {   
  108.                         System.out.print(arr[i]+"   ");   
  109.                 }   
  110.         }   
  111. }  
  112.  
  113.  
  114.  
  115. d.  
  116.  
  117. Java code  
  118.  
  119.  
  120. import   java.util.*;   
  121. public   class   Test   
  122. {   
  123.           
  124.         public   static   void   main(String[]   args)   
  125.         {   
  126.                 Set    mySet   =   new   LinkedHashSet  ();   
  127.                 while(mySet.size() <10)   
  128.                 {   
  129.                         mySet.add((int)(Math.random()*40+1));   
  130.                 }   
  131.                 for(Integer   i:mySet)   
  132.                 {   
  133.                         System.out.print(i+"   ");   
  134.                 }   
  135.         }   
  136. }  
  137.  


方法一:
在一个待选数组中随机产生一个数,然后把他放到待选数组的最后,然后从length-1里随机产生下一个随机数,如此类推

  1. public static int[] randoms()  
  2. {  
  3. Random r = new Random();  
  4.  
  5. int temp1,temp2;  
  6. int send[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};  
  7. int len = send.length;  
  8. int returnValue[] = new int[22];  
  9. for(int i=0;i<22;i++)  
  10. {  
  11. temp1 = Math.abs(r.nextInt())% len;  
  12. returnValue[i] = send[temp1];  
  13. temp2 = send[temp1];  
  14. send[temp1] = send[len-1];  
  15. send[len-1] = temp2;  
  16. len--;  
  17. }  
  18. return returnValue;  
  19. }  

方法二:
还是一个固定的无重复的数组,然后把这个数组随机调换位置,多次之后这个数组就是一个无重复的随机数组了。

  1. public static int[] random2()  
  2. {  
  3.    int send[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};  
  4.    int temp1,temp2,temp3;  
  5.    Random r = new Random();  
  6.    for(int i=0;i//随机交换send.length次  
  7.    {  
  8.     temp1 = Math.abs(r.nextInt())%(send.length-1); //随机产生一个位置  
  9.     temp2 = Math.abs(r.nextInt())%(send.length-1); //随机产生另一个位置  
  10.     if(temp1 != temp2)  
  11.     {  
  12.      temp3 = send[temp1];  
  13.      send[temp1] = send[temp2];  
  14.      send[temp2] = temp3;  
  15.     }  
  16.    }  
  17.    return send;  
  18. }  
  19.  


 

【编辑推荐】

  1. 20个开发人员非常有用的Java功能代码
  2. 走进Java 7中的模块系统
  3. JavaFX 1.2 已经发布 主要新功能一览
  4. 2009年十大Java技术解决方案
  5. 2008最值得学习的五种JAVA技术

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...