A method of generating random numbers in Java

Source: Internet
Author: User

There are three ways to do this:

  1. Math.random (): This method returns a random double type of [0.0, 1.0]. It is actually called the Nextdouble () method of the Random class. But the math class uses a static random number generator (new random ()), which is a thread-safe method, so multiple threads share a random number generator. If many threads are using the random number generator frequently, it is better to assign a random number generator to each thread. In addition, this method does not seem to be supported in J2ME.
  2. The Random class: a powerful pseudo-random number generator provided by Java. There are two methods for constructing methods and the main six classes to get random numbers.
      1. Two construction methods:
        1. random (): Using System Time (System.nanotime (), nanosecond level) as a seed, this random number generator generates random numbers that are always different
        2. random (Long Seed): Using a specific seed, if the two random number generators have the same seed, the same method is called in the same order, and they return the same random number. (In other words, the pseudo-random number is given an initial value, and then a calculation, the next one, the next value.) So once the seed is determined, then all the random number sequences are actually deterministic. So it's called a pseudo-random number. Online poker can sometimes cheat by synchronizing with the server system clock to get the same clock seed, so that it can determine the sequence of random numbers and know all the cards.
      2. class six Gets the random number method: Nextint (), nextdouble (), Nextlong (), Nextboolean (), Nextfloat (), Nextgaussian (). Of course, nextint additional gauge, can be achieved through the nextint (), do not say ~
  3. System.currenttimemillis: This returns the number of milliseconds from January 1, 1970 0:0 0 seconds to the current one. After this time, you can get the random number by taking a modulo (this is actually the generation method of defining the random number, which is what the random class does). Of course, the second type of random constructor can be used as a seed, so the random number of each return is different, because the time of the call must be different.

A method of generating random numbers in Java

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.