nextint (int num) of the Java.util.Random class

Source: Internet
Author: User

randomly generates 3 67~295 integers and finds the number centered and outputs the middle number
Example: 100,225 and 200, output

To randomly produce integers in a range, the nextint (int num) of the Java.util.Random class is the most concise.

nextint (int num) can accept an integer as the upper bound of the random integer it produces, with a lower bound of zero, such as:
Nextint (4) will produce 0,1,2,3 any one of the 4 numbers, note that this is not 0-4, but 0-3.
。 But the lower bound is always zero and cannot be changed, so to achieve the effect of a non-0 lower limit, the upper limit minus the lower bound result must be passed to Nextint (), and then the lower bound is added to the integer returned by Nextint ().

The number of random numbers is collected in the array, and then the array is sorted with the same arrays.sort () in the Java.util packet, and the center count is in the immediate place.

1Import java.util.*;2 3 classC {4       Public Static voidMain (string[] args) {5 6Random Rand =NewRandom ();7          int[] Trio =New int[3 ];8 9System. out. println ("three random integers in the range of [295]:" );Ten           for(inti =0; I <3; ++i) { Onetrio[i] = Rand.nextint (295- the) + the; ASystem. out. println (trio[i]); -          } -  the Arrays.sort (trio); -  -System. out. println ("\nmedian:\n"+ trio[1 ] ); -      } +}

nextint (int num) of the Java.util.Random class

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.