Ideas
Save 1-1000 integers in an array of size 1000, int nums[1000] = {,... 1000}.
Use Random.nextint () to get [0,999] subscript value, that is, index = random.nextint (1000), Exchange coordinates 0 and index value
Use Random.nextint () to get [0,999] subscript value, that is, index = random.nextint (1000), Exchange coordinates 1 and index value
...
Use Random.nextint () to get [0,999] subscript value, that is, index = random.nextint (1000), Exchange coordinates 899 and index value
Last [0-899] saved is 900 x 0-900
ImportJava.util.Random; Public classRandomnums {/*** get n random integers from an integer Min~max *@parammin Random integer minimum value *@parammax random integer maximum value *@paramn Random number of numbers *@returnrandomly generated n integers*/ Public voidGetrandomnums (intMinintMaxintN) { intLength = max-min + 1; if(Max < min | | n >length)return; //put an integer in the range Min~max range into an array intNums[] =New int[length]; for(inti = min; I <= Max; i++) {nums[i-min] =i; }// forRandom Random=NewRandom (); for(inti = 0; I < n;i++){ intindex = Random.nextint (max);//randomly produces an array subscriptExchange (Nums, I, index);//0~n-1 storing n random numbers}// for for(inti = 0; I < n; i++) System.out.println (Nums[i]); } /*** Swap values of two elements in an array *@paramNums *@paramI *@paramJ*/ Public voidExchangeintNums[],intIintj) { inttemp =Nums[i]; Nums[i]=Nums[j]; NUMS[J]=temp; } Public Static voidMain (String args[]) {randomnums randomnums=Newrandomnums (); Randomnums.getrandomnums (1, 1000, 900); }}
Randomly remove 900 non-repeating random numbers from 1 to 1000