JAVA取隨機數的三種方式:
- Math.random()
- System.currentTimeMillis()
- random.nextInt()
廢話不多說,看代碼:

1 /** 2 *取單個隨機數 3 *Math.random()取10~100之間隨機數 4 *Math.round()進行向上取整處理=>{3.5=>4}、{-3.5=>-3} 5 * 6 **/ 7 @Test 8 public void test1(){ 9 int max=100; 10 int min=10; 11 int randomNumber = (int) Math.round(Math.random()*(max-min)+min); 12 System.out.println("10~100之間隨機數:"+randomNumber); 13 } 14 /** 15 * 取單個隨機數 16 * System.currentTimeMillis()返回從1970年1月1號0點0分0秒到目前的一個long型的毫秒數 17 * 通過取模,限制隨機數范圍 18 * */ 19 @Test 20 public void test2(){ 21 int max=100; 22 int min=10; 23 long randomNum = System.currentTimeMillis(); 24 int randomNumber = (int)(randomNum%(max-min))+min; 25 System.out.println("10~100之間隨機數:"+randomNumber); 26 } 27 /** 28 * 取單個隨機數 29 * Random是一個隨機數發生器 30 * 有兩種構造函數:Random()使用當前時間即System.currentTimeMillis()作為發生器的種子 31 * Random(long seed)使用指定的seed作為發生器的種子 32 * 再通過nextInt()、 nextLong()、nextFloat()、nextDouble()等獲得不同類型隨機數 33 * */ 34 @Test 35 public void test3(){ 36 int max=100; 37 int min=10; 38 Random random = new Random(); 39 int randomNumber = random.nextInt(max)%(max-min+1) + min; 40 System.out.println("10~100之間隨機數:"+randomNumber); 41 }
JAVA取N個隨機數的三種方法:
- 雙重循環去重法
- HashSet
- 排除已隨機的數法

1 /** 2 * 取N個不同的隨機數 3 * 1.雙重循環去重法 4 * 進過第一重循環取值,另一重進行比較,去重 5 * */ 6 public int[] randomCommon1(int min ,int max , int n){ 7 int[] result = new int[n]; 8 if(n>max-min+1||min>max){ 9 return null; 10 } 11 int count = 0;//記錄數量 12 while(count<n){ 13 int randomNumber = (int) Math.round(Math.random()*(max-min)+min); 14 boolean falg = true; 15 for(int i=0;i<n;i++){ 16 if(randomNumber == result[i]){ 17 falg = false; 18 break; 19 } 20 } 21 if(falg){ 22 result[count] = randomNumber; 23 count++; 24 } 25 } 26 return result; 27 } 28 /** 29 * 取N個不同的隨機數 30 * 2.HashSet 31 * 使用HashSet只允許存放不同值原理 32 * */ 33 public void randomCommon2(int min,int max,int n,HashSet<Integer> set){ 34 if(n>max-min+1||min>max){ 35 return; 36 } 37 int randomNumber = (int) Math.round(Math.random()*(max-min)+min); 38 set.add(randomNumber); 39 if(set.size()<n){ 40 randomCommon2(min,max,n,set);//遞歸 41 } 42 } 43 /** 44 * 取N個不同的隨機數 45 * 3.排除已隨機的數 46 * */ 47 public static int[] randomArray(int min,int max,int n){ 48 int len = max-min+1; 49 50 if(max < min || n > len){ 51 return null; 52 } 53 54 //初始化給定范圍的待選數組 55 int[] source = new int[len]; 56 for (int i = min; i < min+len; i++){ 57 source[i-min] = i; 58 } 59 60 int[] result = new int[n]; 61 Random rd = new Random(); 62 int index = 0; 63 for (int i = 0; i < result.length; i++) { 64 //待選數組0到(len-2)隨機一個下標 65 index = Math.abs(rd.nextInt() % len); 66 len--;//始終保持取值范圍縮小,並保持替換值在改變 67 //將隨機到的數放入結果集 68 result[i] = source[index]; 69 //將待選數組中被隨機到的數,用待選數組(len-1)下標對應的數替換 70 source[index] = source[len]; 71 } 72 return result; 73 } 74 75 @Test 76 public void Test(){ 77 //方法1: 78 int[] result1 = randomCommon1(0,10,5); 79 for(int i:result1){ 80 System.out.print(i+"#"); 81 } 82 System.out.println(); 83 //方法2: 84 HashSet<Integer> set = new HashSet<Integer>(); 85 randomCommon2(0,10,5,set); 86 for(int i:set){ 87 System.out.print(i+"#"); 88 } 89 System.out.println(); 90 //方法3: 91 int[] result3=randomArray(0,10,5); 92 for(int i:result3){ 93 System.out.print(i+"#"); 94 } 95 }
結果:

1 9#6#4#3#10# 2 1#4#8#9#10# 3 10#9#4#0#5#