Java Random class explanation

Source: Internet
Author: User
Tags abs

Java Random class is located under the Java.util package, mainly used to generate random numbers, this article explains the use of the random class, I hope to help everyone

Random Class (Java.util)

The random algorithm implemented in the random class is pseudo-random, that is, random with rules. 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. This requires special attention when generating multiple random numbers.

The following describes the use of the random class and how to generate a random array of specified intervals and the probability of implementing the requirements in the program.

1. Generation of random objects

The random class contains two construction methods, which are described in turn:

A, public Random ()

The construction method uses a number that corresponds to the relative time of the current system time as the seed number, and then constructs the random object using this seed number.

B, public Random (long Seed)

The construction method can be created by setting a seed number.

Example code:

The code is as follows:
Newnew Random (10);

Again, the seed number is only the origin number of the stochastic algorithm, regardless of the interval of the generated random numbers.

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 between [0,1.0].

c, public int nextint ()

The function of this method is to generate a random int value that is between the range of int, which is 231 to 231-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

The code is as follows:
double D1 = r.nextdouble ();

obtained directly using the Nextdouble method.

b, decimal for generating [0,5.0] Interval

The code is as follows:
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

The code is as follows:
double d3 = r.nextdouble () * 1.5 + 1;

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

The code is as follows:
int n1 = R.nextint ();

You can use the Nextint method directly.

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

The code is as follows:
int n2 = R.nextint (Ten= 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 to generate an arbitrary int number, the number and 10 after the remainder of the generated number range is ( -10,10), and then to the absolute value of the interval, then the resulting interval is [0,10].

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

The code is as follows:
int n2 == Math.Abs (r.nextint ()% n);

F, generating integers for [0,10] intervals

The code is as follows:
int n3 = r.nextint (one= 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

The code is as follows:
int N4 = r.nextint (3) = 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.

H, chance to achieve

The realization of program logic at a certain probability is also a problem that can be solved by random processing. The following is a simple example of how to use random numbers to achieve the probability of logic.

In the previous method introduction, the numbers generated in the nextint (int n) method are uniform, meaning that each number inside the interval has the same probability of being generated. Then if a random integer of [0,100] interval is generated, the odds of each number being generated should be the same, and since there are 100 integers in the interval, the odds of each number are 1%. According to this theory, the probability problem can be realized in the program.

Example: Randomly generates an integer that generates 1 at a 55% chance, generates 2 at 40%, and generates 5% with a 3 chance. The implementation code is as follows:

The code is as follows:
int N5 = r.nextint (+); int // result Numbers if // 55-digit interval, 55% chance    m = 1;} Else if (N5 < 95) {//[55,95], 40-digit interval, 40% chance    m = 2;} Else {    = 3;}

Since the odds of each number are 1%, then the probability of any 55-digit interval is 55%, and for the code to write conveniently, all integers in the [0,55] interval are used, followed by the same principle.

Of course, the code here can be simplified because the odds are multiples of 5%, so as long as 5% is the basis to control the odds, here is the simplified code implementation:

The code is as follows:
int N6 = R.nextint (a); int M1; if (N6 <) {    = 1;} Else if (N6 <) {    = 2;} Else {    = 3;}

Within the program, the probability logic can be implemented as described above.

4. Other issues

A, the same seed number random object problem

As explained earlier, the random number of the same seed number is exactly the same, and the following is the code for the test:

The code is as follows:
New Random (thenew random (ten);  for (int i = 0;i < 2;i++) {     System.out.println (R1.nextint ());     System.out.println (R2.nextint ());}

In this code, the number of seeds used by both the object R1 and R2 is 10, and the random number generated by the same number of objects is exactly the same.

If you want to avoid situations where random numbers are the same, you need to be aware that no matter how many random numbers you need to generate in your project, you can use only one random object.

b, about the random method in the math class

In fact, there is also a random method in the math class, where the work of the random method is to generate a decimal number for a [0,1.0] interval.

By reading the source code of the math class, you can see that the random method in the math class is directly implemented by invoking the Nextdouble method in the random class.

The call to the random method is simple, so many programmers are accustomed to using the math class's random method to generate random numbers.

Original link: http://www.jb51.net/article/83028.htm

Java Random class explanation

Related Article

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.