Java generates random numbers within a specified range

Source: Internet
Author: User

  We often say that random numbers in Java are pseudo-random numbers, so we need to know what pseudo-random numbers are.

What is a pseudo-random number?

1, the real meaning of the random number in a certain generation process is in accordance with the distribution of the probability of the occurrence of random, the results are unpredictable, is not visible. This is really random.

2, the computer random function is based on the determination algorithm simulation, the result is determined, is visible, we can think of this predictable result of its occurrence probability is 100%, so called pseudo-random.

Java random number generation principle :

  The random number generation of Java uses a linear congruence formula, which means that it is generated by a complex algorithm. If interested, you can go to the Java random number of source code parsing blog. http://www.codes51.com/article/detail_1581162_5.html

There are two ways in which Java generates random numbers:

1, Math.random ()
2. New Random ()

java.lang.Math.Random:

  The Math.random () function can return a double value between 0-1, evenly distributed within the range (approximate).

Java.util.Random:

There are two ways to construct Random ():

1. Random (): The number of milliseconds in the current time can be obtained by System.currenttimemillis () using the number of milliseconds of the current time as a seed. Here is the source code for the JDK.


Public Random () {This (System.currenttimemillis ());}.

2. Random (long Seed): pass in a seed directly, if you create two Random instances with the same seed, make the same method call sequence for each instance, and they will generate and return the same sequence of numbers.

Nextint () of the Random object, nextint (int n) Method:
int Nextint ()
Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator.
int nextint (int n)
Returns a pseudo-random number, which is an int value that is taken out of the sequence of this random number generator and evenly distributed between [0,1].

Java generates random numbers within a specified range

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.