日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java產(chǎn)生不重復(fù)隨機(jī)數(shù)方法

關(guān)于生成Java不重復(fù)的隨機(jī)數(shù):

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供隆陽企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、H5建站、小程序制作等業(yè)務(wù)。10年已為隆陽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。

 
 
 
  1. import java.util.*;
  2. public class Test...{
  3.     public static void main(String[] args)...{
  4.         //生成 [0-n) 個(gè)不重復(fù)的隨機(jī)數(shù)
  5.         / st 用來保存這些隨機(jī)數(shù)
  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.     
  18.             do...{
  19.                 //如果產(chǎn)生的數(shù)相同繼續(xù)循環(huán)
  20.                 num = rand.nextInt(n);    
  21.              
  22.             }while(bool[num]);
  23.             
  24.             bool[num] =true;
  25.             
  26.             list.add(num);
  27.         
  28.         
  29.         }
  30.             
  31.     
  32.         System.out.println (list);    
  33. }        
  34.         
  35. public class Test
  36. {
  37.     public static void main(String[] args)
  38.     {
  39.         int[] arr = new int[10];
  40.         for (int i = 0; i < 10; i++)
  41.         {
  42.             arr[i] = (int) (Math.random() * 40) + 1;
  43.             for (int j = 0; j < i; j++)
  44.             {
  45.                 if (arr[j] == arr[i])
  46.                 {
  47.                     i--;
  48.                     break;
  49.                 }
  50.             }
  51.         }
  52.         for (int i = 0; i < 10; i++)
  53.             System.out.print(arr[i] + " ");
  54.     }
  55. }
  56. b.
  57. Java code
  58. import   java.util.*; 
  59. public   class   Test 
  60.         
  61.         public   static   void   main(String[]   args) 
  62.         { 
  63.                 int   n=40; 
  64.                 int[]   num   =   new   int[n]; 
  65.                 for(int   i=0;i 
  66.                         num[i]   =   i+1; 
  67.                 int[]   arr   =   new   int[10]; 
  68.                 for(int   i=0;i 
  69.                 { 
  70.                         int   r   =(int)(Math.random()*n); 
  71.                         arr[i]=num[r]; 
  72.                         num[r]=num[n-1]; 
  73.                         n--; 
  74.                 } 
  75.                 for(int   i=0;i 
  76.                         System.out.print(arr[i]+"   "); 
  77.         } 
  78. }
  79. c.
  80. Java code
  81. import   java.util.*; 
  82. public   class   Test 
  83.         
  84.         public   static   void   main(String[]   args) 
  85.         { 
  86.                 LinkedList    myList=   new   LinkedList  (); 
  87.                 int   n=40; 
  88.                 for(int   i=0;i 
  89.                         myList.add(i+1); 
  90.                 int[]   arr   =   new   int[10]; 
  91.                 for(int   i=0;i 
  92.                 { 
  93.                         arr[i]=myList.remove((int)(Math.random()*n)); 
  94.                         n--; 
  95.                 } 
  96.                 for(int   i=0;i 
  97.                 { 
  98.                         System.out.print(arr[i]+"   "); 
  99.                 } 
  100.         } 
  101. }
  102. d.
  103. Java code
  104. import   java.util.*; 
  105. public   class   Test 
  106.         
  107.         public   static   void   main(String[]   args) 
  108.         { 
  109.                 Set    mySet   =   new   LinkedHashSet  (); 
  110.                 while(mySet.size() <10) 
  111.                 { 
  112.                         mySet.add((int)(Math.random()*40+1)); 
  113.                 } 
  114.                 for(Integer   i:mySet) 
  115.                 { 
  116.                         System.out.print(i+"   "); 
  117.                 } 
  118.         } 
  119. }

方法一:
在一個(gè)待選數(shù)組中隨機(jī)產(chǎn)生一個(gè)數(shù),然后把他放到待選數(shù)組的最后,然后從length-1里隨機(jī)產(chǎn)生下一個(gè)隨機(jī)數(shù),如此類推

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

方法二:
還是一個(gè)固定的無重復(fù)的數(shù)組,然后把這個(gè)數(shù)組隨機(jī)調(diào)換位置,多次之后這個(gè)數(shù)組就是一個(gè)無重復(fù)的隨機(jī)數(shù)組了。

 
 
 
  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
  7.    {
  8.     temp1 = Math.abs(r.nextInt())%(send.length-1); //隨機(jī)產(chǎn)生一個(gè)位置
  9.     temp2 = Math.abs(r.nextInt())%(send.length-1); //隨機(jī)產(chǎn)生另一個(gè)位置
  10.     if(temp1 != temp2)
  11.     {
  12.      temp3 = send[temp1];
  13.      send[temp1] = send[temp2];
  14.      send[temp2] = temp3;
  15.     }
  16.    }
  17.    return send;
  18. }

網(wǎng)站題目:Java產(chǎn)生不重復(fù)隨機(jī)數(shù)方法
文章網(wǎng)址:http://www.5511xx.com/article/cdhjihe.html