1 Public classRandomnumber {2 /**3 * Generate a random number with a specified number of repetitions within a given range4 * @parammin within the given range5 * @parammax value in given range6 * @paramnumber generates a random number7 * @return8 */9 Public Static int[] Createrandomnumber (intMinintMaxintNumber ) {Ten if(Number > max-min + 1 | | number < 1 | | Max <min) { One return NULL; A } - int[] arr =New int[number]; -Random r =NewRandom (); the intCount = 0; - while(Count <Number ) { - intRandomnumber = R.nextint (max-min + 1) +min; - BooleanFlag =true;//This sentence is very important, can not be written in the while outside, or it is the cycle of death. + for(intj = 0; J < number; J + +) { - if(Arr[j] = =Randomnumber) { +Flag =false; A Break; at } - } - if(flag) { -Arr[count] =Randomnumber; -count++; - } in } - to returnarr; + } - the Public Static voidMain (string[] args) { * int[] arr = Createrandomnumber (4, 10, 7); $ for(inti = 0; i < arr.length; i++) {Panax Notoginseng System.out.println (Arr[i]); - } the } +
Produces a specified number of random numbers that are not duplicated within a given range