Java gets a random number in a range

Source: Internet
Author: User

First, take the mold operation

 Public Static void Main (string[] args) {   for (int i = 1; I <=; i++) {  int j = i% 11
   
    + "%11 Results--" +
     J); }}/
    *
    1%11 results--12%11 results--23%11 results--34%11 results--45%11 results--56%11 results--67%11 Results--78%11 Results of the result of 11--910%11 results of--1011%11 results of--012%11--113%11 results--214%11 results--315%11 results--416%11 results--517%11 Results--618%11 Results of%11--820%11 results--9
    * /
   

Second, Java.uitl.Random

Random.nextint (20), arbitrarily fetch the integer between [0,20], where 0 can be taken, and 20 cannot be taken.

Iii. taking any number of a range

 Public Static String getrandom (intint  max) {    new  Random ();     int s = random.nextint (max)% (Max-min + 1) + min;     return string.valueof (s);} /* principle: The range of random numbers to be obtained is [2,100], assuming that the range of pseudo-random numbers returned is [0,n], that is, [0,n-1]; for the obtained modulo 99, the range of the calculated number is [0,98], and the result is added 2, the range is [2,100]. */

Java gets a random number in a range

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.