Java learning about the random number tool class--random class

Source: Internet
Author: User

The random class is a pseudo-random number generator. They are called pseudo-random numbers (pseudorandom) because they are simply uniformly distributed sequences. The random class defines the following constructors:

Random ()

Random (Long Seed)

The random number generator created with the first version uses a relatively unique seed (the current time). The second version allows you to specify the seed manually. If you initialize the random object with a seed, the start point is defined for the random sequence. If you initialize another random object with the same seed, you get the same random sequence. If you want to generate a different sequence, you need to specify a different seed. One way to achieve this is to use the current time as the seed of the random object. This approach reduces the likelihood of repeating sequences.

The core public method defined by the random class:

return value Method Describe
Boolean Nextboolean () Returns the next Boolean random number
void Nextbytes (byte vals[]) Populate Vals with randomly generated values
Double Nextdouble () Returns the next double type random number
Float Nextfloat () Returns the next float type random number
Double Nextgaussian () Returns the next Gaussian distribution random number
Int Nextint () Returns the next int type random number
Int Nextint (int n) Returns the next int type random number between 0 and N
Long Nextlong () Returns the next long type random number
void Setseed (Long Newseed) Set the value specified by Newseed to seed

You can extract multiple types of random numbers from the random object, which can be obtained by Boolean, Byte, Integer, and float. In addition, the Nextgaussian () method returns a standard Gaussian distribution random number with a mean of 0.0 and a variance of 1.0, with a type of double, which is known as the bell curve. The following example shows a random sequence produced by the Nextgaussian () method. First, 100 random Gaussian values are obtained and their averages are calculated. Then, in 0.5 units, the number of values that fall within the range of plus and minus two standard deviations is counted.

Java learning about the random number tool class--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.