Java Learning (22): How to generate random numbers

Source: Internet
Author: User

How Java random numbers are generated
In Java, the concept of random numbers is broadly based, with three of them.
1, through System.currenttimemillis () to obtain a current time milliseconds number of long.
2. Returns a double value between 0 and 1 through Math.random ().
3, through the random class to produce a random number, this is a professional random tool class, powerful.

Java.util.Random class to produce a random number generator. It has two forms of constructors, namely random () and random (long Seed).
Random () This construction method sets the seed of the random number generator to a value that is completely different from the values used by all other invocations of this construction method (the article on the web says that the System.currenttimemillis () is used as a seed, the measurement is not deterministic, Maybe not a simple use of the current time).
The Random (long Seed) uses the specified seed as the seed for the generator. The same seed produces the same random number.
After the random number generator (randomness) object is produced, different types of random numbers are obtained by calling different Method:nextint (), Nextlong (), Nextfloat (), nextdouble (), and so on.

1  Public Static voidMain (string[] args)2     {3          for(inti = 0; I < 10; i++)4         {5Random Rand =NewRandom ();6System.out.println ("random number" + i + ":" +rand.nextint ());7System.out.println ("Time:" +System.currenttimemillis ());8         }9}

As you can see from the printout, the random numbers generated at the same time are not the same:

Random number 0:428,923,238
time:1446719287485
Random number 1:1644891094
time:1446719287485
Random number 2:2063294173
time:1446719287485
Random Number 3:220798588
time:1446719287485
Random number 4:1036960660
time:1446719287485
Random number 5:2027516416
time:1446719287485
Random number 6:705,879,658
time:1446719287485
Random number 7:388,111,823
time:1446719287485
Random number 8:1,685,942,599
time:1446719287485
Random Number 9:1417505914
time:1446719287485

Java Learning (22): How to generate 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.