一.JDK中提供的兩種方式
1.Math類中的random()方法
public static double random()
返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0 [0.0,1.0)。返回值是一個偽隨機選擇的數,在該范圍內(近似)均勻分布。
2.Random類中的 nextInt()方法
public int nextInt()
返回一個隨機數,所有 2的32次方個可能 int 值的生成概率(大致)相同。
public int nextInt(int n)
返回一個偽隨機數,它是取自此隨機數生成器序列的、在 0(包括)和指定值(不包括)之間均勻分布的 int 值。
nextInt 的常規協定是,偽隨機地生成並返回指定范圍中的一個 int 值。所有可能的 n 個 int 值的生成概率(大致)相同。
參數:
n - 要返回的隨機數的范圍。必須為正數。
返回:
隨機數生成器序列中 0(包括)和 n(不包括)之間 [0,n) 均勻分布的 int 值。
拋出:
IllegalArgumentException - 如果 n 不是正數
StackOverFlow總結的的經典的回答:
二.隨機獲取制定范圍的制定個數的隨機數
上代碼:
1 import java.util.HashSet; 2 import java.util.Random; 3 4 public class GenerateRandomNumber { 5 6 public static void main(String[] args) { 7 //一、JAVA中生成隨機數的方式 8 //① 9 //int randomNumber = (int) Math.round(Math.random()*(max-min)+min); 10 11 //② 12 //long randomNum = System.currentTimeMillis(); 13 //int randomNumber = (int) randomNum%(max-min)+min; 14 15 //③ 16 //Random random = new Random(); 17 //int randomNumber = random.nextInt(max)%(max-min+1) + min; 18 19 //方法一:最簡單最易理解的兩重循環去重 20 // int[] reult1 = randomCommon(20, 50, 10); 21 // for (int i : reult1) { 22 // System.out.println(i); 23 // } 24 25 //方法二:利用HashSet的特征,只能存放不同的值 26 // HashSet<Integer> set = new HashSet<Integer>(); 27 // randomSet(20, 50, 10, set); 28 // for (int j : set) { 29 // System.out.println(j); 30 // } 31 32 //方法三:排除已隨機到的數 33 int[] reult2 = randomArray(0, 20, 18); 34 // for (int i : reult2) { 35 // System.out.println(i); 36 // } 37 } 38 39 //二、隨機給定范圍內N個不重復的數 40 /** 41 * 方法一:最簡單最易理解的兩重循環去重 42 * 隨機指定范圍內N個不重復的數 43 * 最簡單最基本的方法 44 * @param min 指定范圍最小值 45 * @param max 指定范圍最大值 46 * @param n 隨機數個數 47 */ 48 public static int[] randomCommon(int min, int max, int n){ 49 if (n > (max - min + 1) || max < min) { 50 return null; 51 } 52 int[] result = new int[n]; 53 int count = 0; 54 while(count < n) { 55 int num = (int) (Math.random() * (max - min)) + min; 56 boolean flag = true; 57 for (int j = 0; j < n; j++) { 58 if(num == result[j]){ 59 flag = false; 60 break; 61 } 62 } 63 if(flag){ 64 result[count] = num; 65 count++; 66 } 67 } 68 return result; 69 } 70 71 /** 72 * 方法二:利用HashSet的特征,只能存放不同的值 73 * 隨機指定范圍內N個不重復的數 74 * 利用HashSet的特征,只能存放不同的值 75 * @param min 指定范圍最小值 76 * @param max 指定范圍最大值 77 * @param n 隨機數個數 78 * @param HashSet<Integer> set 隨機數結果集 79 */ 80 public static void randomSet(int min, int max, int n, HashSet<Integer> set) { 81 if (n > (max - min + 1) || max < min) { 82 return; 83 } 84 for (int i = 0; i < n; i++) { 85 // 調用Math.random()方法 86 int num = (int) (Math.random() * (max - min)) + min; 87 set.add(num);// 將不同的數存入HashSet中 88 } 89 int setSize = set.size(); 90 // 如果存入的數小於指定生成的個數,則調用遞歸再生成剩余個數的隨機數,如此循環,直到達到指定大小 91 if (setSize < n) { 92 randomSet(min, max, n - setSize, set);// 遞歸 93 } 94 } 95 96 /** 97 * 方法三:排除已隨機到的數 98 * 隨機指定范圍內N個不重復的數 99 * 在初始化的無重復待選數組中隨機產生一個數放入結果中, 100 * 將待選數組被隨機到的數,用待選數組(len-1)下標對應的數替換 101 * 然后從len-2里隨機產生下一個隨機數,如此類推 102 * @param max 指定范圍最大值 103 * @param min 指定范圍最小值 104 * @param n 隨機數個數 105 * @return int[] 隨機數結果集 106 */ 107 public static int[] randomArray(int min,int max,int n){ 108 //min=20 max=40 n=10 109 int len = max-min+1; 110 111 if(max < min || n > len){ 112 return null; 113 } 114 115 //初始化給定范圍的待選數組 116 int[] source = new int[len]; 117 for (int i = min; i < min+len; i++){ 118 source[i-min] = i; 119 } 120 //source 初始化之后就是min~max(包括首尾) 之間的所有數字的一個數組.[20,21...,10] 121 122 int[] result = new int[n]; 123 Random random = new Random(); 124 int index = 0; 125 for (int i = 0; i < result.length; i++) { 126 System.out.println("第"+i+"次循環"); 127 128 int randomInt = random.nextInt(); 129 int maxEffectiveArraySubScript = len--;//最大有效數組下標,數組中超過這個下標的值都沒有用了... 130 //待選數組0到(len-2)隨機一個下標 除數(maxEffectiveArraySubScript) 取余 范圍為(0~maxEffectiveArraySubScript-1) 131 //即(0~maxEffectiveArraySubScript-1) <==> 0到(len-2) 132 index = Math.abs(randomInt % maxEffectiveArraySubScript ); 133 134 System.out.println("randomInt:"+randomInt+",原數組最大有效角標:"+maxEffectiveArraySubScript+",index:"+index); 135 System.out.println("原數組有效檢索子數組:"); 136 printArray(source,maxEffectiveArraySubScript); 137 System.out.println(""); 138 139 //將隨機到的數放入結果集 140 result[i] = source[index]; 141 System.out.println("找到的數為:"+result[i]); 142 //將待選數組中被隨機到的數,用待選數組(len-1)下標對應的數替換 ,這樣確保"有效檢索子數組"中后面沒有檢索到的數據前移,下一次仍可能隨機找到. 143 //因為每一次循環 原數組的有效長度都是-1的... 144 source[index] = source[len]; 145 146 System.out.println("原數組變成:"); 147 printArray(source); 148 System.out.println(""); 149 System.out.println("目標數組為:"); 150 printArray(result); 151 System.out.println(""); 152 } 153 return result; 154 } 155 156 public static void printArray(int [] intArray){ 157 for (int i = 0; i < intArray.length; i++) { 158 System.out.print(intArray[i]+","); 159 } 160 } 161 162 public static void printArray(int [] intArray,int maxArrayNum){ 163 for (int i = 0; i < maxArrayNum; i++) { 164 System.out.print(intArray[i]+","); 165 } 166 } 167 }
方法三:相對有點不好理解,我加上了打印內容.方便理解.
