Randomly remove 900 non-repeating random numbers from 1 to 1000

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.