Use it--the probability of using and generating random numbers

Source: Internet
Author: User

The random class, which is used to generate stochastic numbers for a specified interval, has a constructor with no parameters and two parameters, with parameters of the constructor whose parameters are 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 integers for [0,n] intervals (two kinds)

int i= r.nextint (n+1);

N3 = Math.Abs (R.nextint ()% (n+1));

3. generating integers for [0,n] intervals

int i= r.nextint (n);

4. Generate decimals for [0,1.0] Interval

Double d = r.nextdouble ();

5. Generate decimals for [1,2.5] Interval

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

It is worth noting that if the constructor of R is carrying parameters

Random r = new Random (n)

The parameter n is a long type, called the seed number, and the random object that has the same number of seeds, the same number of occurrences is exactly the same.

Tests such as


Therefore, in the actual project, only one can be used.

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




Use it--the probability of using and generating random numbers

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.