Generation random number of Java learning

Source: Internet
Author: User

The method in 1.Java random () can be used to generate a random number, called a pseudo-random number generator, which returns a number greater than or equal to 0.0, less than 1.0 (double type), or 0.0<=x<1.0. The resulting number is called a pseudo-random number because it is not really random. When we call this method repeatedly, the resulting number is periodically duplicated. Therefore, in theory, the number generated is not random, but for practical applications it is enough to be random.

But for most applications, the random number we want to generate is an integer. So we need to convert the value of this return and expect it to fall within the range we need. Suppose that the integer range we need is [min, Max], and if X is the returned random number, you can use the following formula to convert it to Y so that y falls within the interval [min, Max], which is min<=y<=max.

Y = [X * (max-min+1)] + min

For many applications, the value of min is 1, so the above formula can be simplified to:

Y = (X * max) + 1

You can use the following statement to express the general formula above:

int randomnumber = (int) (Math.floor (Math.random () * (Max-min + 1)) + min);

Example (1): Let's write a small program to pick a winner of the annual Spring Ballroom dance Party, where participants will get numbers such as m+1, m+2, m+3 and so on to enter the venue. The starting value of the number m is determined by the Chairman of the party, and if there are N contestants, the last number is m+n. At the end of the party, we will run this program and randomly select a winner from M+1 to M+n:

1 ImportJava.util.*;2 3 classrandom_test{4      Public Static voidMain (string[] args) {5         intStartingNumber, Count, Winningnumber, Min, Max;6         7Scanner scan =NewScanner (system.in);8         9System.out.print ("Enter The starting number:");TenStartingNumber =scan.nextint (); One          ASystem.out.print ("Enter The number of the party goers:"); -Count =scan.nextint (); -          theMin = startingnumber + 1; -max = StartingNumber +count; -          -Winningnumber = (int) (Math.floor (Math.random () * (Max-min + 1)) +min); +          -System.out.print ("\nthe Winning number is" +winningnumber); +     } A}

Run the program results to match the expected:

The random class in 2.Java generates an instance of randomness, using the Nextint (int) method in the Random class. For example, random random = new Random (), Code random.nextint (100) represents a randomly generated integer between 0-100, including 0 but excluding 100, and the following example randomly obtains 5 [1,100] values:

1 ImportJava.util.*;2 3 classrandom_test{4      Public Static voidMain (string[] args) {5Random random =NewRandom ();6          for(inti=0; i<5; i++) {7System.out.print (Random.nextint) + 1 + "\ n");8         }9     }Ten}

The results of the operation are as expected:

Generation interval [34,67] random number can be expressed as: Random.nextint (34) +34

Can be summed up as:

int randomnumber = random.nextint (max-min + 1) + min;

Generation random number of Java learning

Related Article

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.