Java generates random numbers

Source: Internet
Author: User
Tags abs

There are generally two random numbers in Java, one is the random () method in math, and the other is the random class.

First, Math.random ()

The decimal number of the 0<x<1 is generated.

Example: How to write, generate randomly generate one of the 0~100 in the number?

Random number between 20-80: (int) ((80-20) *math.random ()) + (20-0) i.e.:. (Max-min) *math.random () + min-0

Math.random () returns only decimals from 0 to 1, if you want 50 to 100, zoom in 50 times times, that is, between 0 and 50, this is still a decimal, and if you want an integer, cast int, then add 50 to 50~100.
Final code: (int) (Math.random () *50) + 50

Second, the Random class

Random random = new random ();//default constructor method

Random random = new random (1000);//Specify seed number

At random, the origin of the random algorithm is called the seed number (seed), the number of seeds on the basis of a certain transformation, resulting in the need for random numbers.

Random objects of the same seed number are identical in number. That is, two random objects with the same number of seeds, the first generation of randomly generated numbers exactly the same, the second generation of random numbers are exactly the same.

2. Common methods in random class

The methods in the Random class are relatively simple, and the functionality of each method is easy to understand. It should be stated that the random numbers generated by each method in the random class are evenly distributed, that is, the probability that the numbers within the interval are equal. Here's a basic introduction to these methods:

A, public boolean nextboolean ()

The function of this method is to generate a random Boolean value that generates a value of true and false that is equal to the probability of 50%.

B, public double nextdouble ()

The function of this method is to generate a random double value, where the value is between [0,1.0], where the brackets represent the end of the interval, and the parentheses represent a random fraction between 0 and 1, which contains 0 without 1.0.

c, public int nextint ()

The function of this method is to generate a random int value, which is between the interval of int, that is, 2 of the 31 to the 2 of the 31-time square-1.

If you need to generate an int value for a specified interval, you need to make some mathematical transformations, as shown in the following example of using code.

d, public int nextint (int n)

The function of this method is to generate a random int value that is in the interval of [0,n], which is a random int value between 0 and N, containing 0 without n.

If you want to generate an int value for a specified interval, you also need to make some mathematical transformations, see the code in the example below.

E, public void setseed (long Seed)

The function of this method is to reset the number of seeds in the random object. The random object that is set after the seed count and the same seed number are created with the same random object as the New keyword.

3. Example of using the random class

Using the random class, which is usually generated by randomly generating a specified interval, describes how to generate random numbers for the corresponding interval. The following code that generates random numbers is generated using the following random object R:

Random r = new Random ();

A, the decimal number of the generation [0,1.0] Interval

Double D1 = r.nextdouble ();
obtained directly using the Nextdouble method.

b, decimal for generating [0,5.0] Interval

Double D2 = r.nextdouble () * 5;

Because the number interval generated by the Nextdouble method is [0,1.0], expanding the interval by 5 times times is the required interval.

Similarly, generating random decimals for the [0,d] interval, and d for any positive decimal, you only need to multiply the return value of the Nextdouble method by D.

c, the decimal number of the generation [1,2.5] interval [N1,N2]

Double d3 = r.nextdouble () * 1.5 + 1; "That is r.nextdouble () * (N2-N1) +n1"

To generate random decimals for the [1,2.5] interval, you only need to generate a random number for the [0,1.5] interval first, and then add 1 to the generated random number interval.

Similarly, to generate random numbers (where D1 is not equal to 0) for any range of fractional ranges [D1,D2] that are not starting at 0, you only need to generate a random number for the [0,d2-d1] interval first, and then add the generated random number interval to D1.

d, generate any integer

int n1 = R.nextint ();

You can use the Nextint method directly.

E, the integer that generates the [0,10] Interval

int n2 = R.nextint (10);

N2 = Math.Abs (r.nextint ()% 10);

Both lines of code can generate integers for the [0,10] interval.

The first implementation is implemented directly using the nextint (int n) method in the Random class.

In the second implementation, the first call to the Nextint () method generates an arbitrary int number, the number and the number interval generated after 10 is ( -10,10), because according to the mathematical provisions of the remainder of the absolute value is less than the divisor, and then the interval to be absolute, then the resulting interval is [0,10].

Similarly, a random integer that generates any [0,n] interval can use the following code:

int n2 = R.nextint (n);

N2 = Math.Abs (r.nextint ()% n);

F, generating integers for [0,10] intervals

int n3 = R.nextint (11);

N3 = Math.Abs (R.nextint ()% 11);

The [0,10] interval and the [0,11] interval are equivalent to the integer interval, so the integer of the [0,11] interval is generated.

g, generating an integer for the [ -3,15] Interval

int N4 = R.nextint (18)-3; "That is r.nextint () * (N2-N1) +n1" N1 is a negative

N4 = Math.Abs (R.nextint ()% 18)-3;

To generate a random integer that is not starting at 0, you can see a description of how the fractional interval implementation is not starting from 0 above.

Java generates 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.