Random Number random in Java

Source: Internet
Author: User
Tags ranges

From: http://lehsyh.iteye.com/blog/646658

Java generally has two random numbers. One is the random () method in math and the other is the random class.

 

I. Math. Random ()

Then a decimal number of 0 <x <1 is generated.

Instance: how to write, generate a random generation of 0 ~ How many of them are in 100?

Math. the returned result of random () is only a decimal number from 0 to 1. If 50 to 100 is required, the value is first increased to 50 times, that is, the value ranges from 0 to 50. Here it is a decimal number. If an integer is required, convert int forcibly, and then add 50 to 50 ~ 100.
Final Code: (INT) (math. Random () * 50) + 50

 

Ii. Random class

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

Random random = new random (1000); // specify the seed number

When a random algorithm is executed, the numbers of the origins of the random algorithm are called seed. A certain number is transformed based on the number of seeds to generate the required random numbers.

For a random object with the same number of seeds, the random numbers generated by the same number of times are identical. That is to say, two random objects with the same number of seeds have identical random numbers generated for the first time and identical random numbers generated for the second time.

 

2. Common methods in the random class

Methods In the random class are relatively simple, and the functions of each method are easy to understand. It should be noted that the random numbers generated by each method in the random class are evenly distributed, that is, the number generation probability within the range is equal. Below are some basic introductions to these methods:

A, public Boolean nextboolean ()

This method is used to generate a random Boolean value. The probability of true and false values generated is equal, that is, the probability is 50%.

B. Public double nextdouble ()

The function of this method is to generate a random double value between [0, 1.0). Here, brackets indicate that the range endpoint is included, and parentheses indicate that the range endpoint is not included, that is, the random decimal number between 0 and 1, which contains 0 and does not contain 1.0.

 

C, public int nextint ()

The function of this method is to generate a random int value between the int range, that is, the 31 Power of-2 to the 31 power of 2-1.

If you need to generate the int value of the specified range, you need to perform a certain mathematical transformation. For details, see the Code in the following example.

D. Public int nextint (int n)

The function of this method is to generate a random int value, which is an interval between [0, n), that is, a random int value between 0 and N, contains 0, not n.

If you want to generate the int value of the specified range, you also need to perform a certain mathematical transformation. For details, see the Code in the following example.

E. Public void setseed (long seed)

This method is used to reset the number of seeds in the random object. The random object after the number of seeds is set is the same as the random object created with the new keyword for the same number of seeds.

3. Random usage example

The random class is generally used to generate random numbers for the specified range. The following describes how to generate random numbers for the corresponding range. The following Random Number Generation Code uses the following random object R:

Random r = new random ();

A. Generate Decimals in the range [0, 1.0)

Double d1 = R. nextdouble ();
Directly use the nextdouble method.

B. Generate Decimals in the range [0, 5.0)

Double D2 = R. nextdouble () * 5;

Because the number range generated by the nextdouble method is [0, 1.0), it is required to increase the range by 5 times.

 

Similarly, generate a random decimal number in the [0, d) interval. If D is an arbitrary positive decimal number, multiply the return value of nextdouble by D.

C. Generate the decimal places in the range [1, 2.5) [N1, N2]

Double D3 = R. nextdouble () * 1.5 + 1; [that is, R. nextdouble () * (n2-n1) + N1]

To generate a random decimal number in the range [1, 2.5), you only need to first generate a random number in the range [0, 1.5), and then add 1 to the random number range.

Similarly, to generate random numbers in the range of any decimal range [D1, D2) not starting from 0 (where D1 is not equal to 0), you only need to first generate [0, d2-d1) then add the generated random number range to the D1.

D. generate any integer

Int n1 = R. nextint ();

Use the nextint method directly.

E. generate an integer in the range [).

Int n2 = R. nextint (10 );

N2 = math. Abs (R. nextint () % 10 );

The above two lines of code can generate an integer in the [0, 10) range.

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

In the second implementation, call the nextint () method to generate an arbitrary int number. The value ranges from-10 to 10 ), because according to the mathematical rule, the absolute value of the remainder is smaller than the divisor, and then the absolute value is obtained for this interval, the obtained range is.

Similarly, the following code can be used to generate random integers in any [0, n) range:

Int n2 = R. nextint (N );

N2 = math. Abs (R. nextint () % N );

F. generate an integer in the range [0, 10 ].

Int N3 = R. nextint (11 );

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

The range [] is equivalent to the range [). Therefore, an integer from [) is generated.

G. generate an integer in the range [-).

Int N4 = R. nextint (18)-3; [that is, R. nextint () * (n2-n1) + N1] N1 is a negative number

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

Generate a random integer that is not in the range starting from 0. For details, see the implementation principle of the fractional interval that is not in the range starting from 0.

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.