Java Programming Ideas Random (47)

Source: Internet
Author: User

The random class contains two construction methods, which are described in turn:
1. Public Random ()
The construction method uses a number that corresponds to the relative time of the current system time as the seed number, and then constructs the random object using this seed number.
2. Public Random (Long Seed)
The construction method can be created by setting a seed number.
Example code:
Random r = new Random ();
Random r1 = new Random (10);
Random r2 = new random (10);
It is emphasized that the number of seeds is only the origin number of the random algorithm, regardless of the interval of the generated random numbers. Random objects of the same seed number are identical in number. So no matter how many random numbers you need to generate in your project, you can use just one random object, namely:
R1.nextint ();
R2.nextint ();
The random number returned at the first or (same time) call is the same, but
for (int i = 0;i < 2;i++) {
System.out.println (R1.nextint ());
}
Generates two different random numbers

Java Programming Ideas Random (47)

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.