Use -- random number usage and probability of generation, random number Probability

Source: Internet
Author: User

Use -- random number usage and probability of generation, random number Probability

The Random class is used to generate Random numbers within a specified range. The constructor has two types of constructor without parameters and parameters. The constructor with parameters has the number of seeds.

Assume that the following r is a Random object

Random r = new Random ();

1. generate any integer

Int I = r. nextInt ();

2. generate an integer in the range [0, n] (two types)

Int I = r. nextInt (n + 1 );

N3 = Math. abs (r. nextInt () % (n + 1 ));

3. generate an integer in the [0, n) Interval

Int I = r. nextInt (n );

4. Generate decimal places in the range [0, 1.0)

Double d = r. nextDouble ();

5. Generate decimal places in the range [1, 2.5)

Double d = r. nextDouble () * 1.5 + 1;

It is worth noting that if the r constructor carries Parameters

Random r = new Random (n)

The n parameter is of the long type. It is called the number of seeds. a Random number is generated with the same number of seeds.

Test as shown in Figure


Therefore, you can use only one project.

In addition, Math also provides a function random () to generate a pseudo-random double value greater than or equal to 0.0 and less than 1.0.




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.