Key knowledge points: 1. (iii) Significance of seed; 2. generates integers or Decimals in any interval. 3. use ramdom to simulate the probability problem. jdk api 1.6.0 explains the following information: Java. util
Class random
java.lang.Object java.util.Random
public class Random
extends Object
-
Implements serializable
This type of instance is used to generate a pseudo-random number stream.
Constructor SummaryRandom () creates a new random number generator. Random (long seed) uses a single
long
Seed to create 0 random number generator. You can reset the seed value through setseed (long.
Method Summary |
protected int |
next(int bits) Generate the next pseudo-random number. |
boolean |
nextBoolean() Returns the next pseudo-random number, which is evenly distributed from the sequence of the random number generator.boolean Value. |
void |
nextBytes(byte[] bytes) Generate random bytes and place them in the byte array provided by the user. |
double |
nextDouble() Returns the next pseudo-random number, which is from the sequence of the random number generator0.0 And1.0 Evenly distributeddouble Value. |
float |
nextFloat() Returns the next pseudo-random number, which is from the sequence of the random number generator0.0 And1.0 Evenly distributedfloat Value. |
double |
nextGaussian() Returns the next pseudo-random number, which is a Gaussian ("normal") distribution derived from the sequence of the random number generator.double Value. The average value is0.0 , Standard deviation is1.0 . |
int |
nextInt() Returns the next pseudo-random number, which is evenly distributed in the sequence of the random number generator.int Value. |
int |
nextInt(int n) Returns a pseudo-random number, which is evenly distributed between 0 (inclusive) and the specified value (excluded) from the sequence of the random number generator.int Value. |
long |
nextLong() Returns the next pseudo-random number, which is evenly distributed from the sequence of the random number generator.long Value. |
void |
setSeed(long seed) Use a singlelong Seed: Specifies the seed of the random number generator. |
Ramdom
Random class (Java. util)
The random algorithm implemented in the random class isPseudo-RandomThat is, there are random rules. When performing a random operationOrigin numberCalledSeed)To generate random numbers based on the number of seeds.
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. This requires special attention when generating multiple random numbers. The following describes how to use the random class and
Generate a random array of the specified rangeAnd
Probability.
1. Generate a random object
The random class contains two constructor methods, which are described in sequence below:
A. Public random ()
This constructor usesRelative time-related number corresponding to the current system timeAs the number of seeds, and then use this number to construct the random object.
B. Public random (long seed)
This constructor can be created by creating a number of seeds.
Sample Code:
Random r = new random ();
Random R1 = new random (10 );
Emphasize again: The number of seeds is only the original number of the random algorithm and is irrelevant to the interval of the generated random number.
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 ()
This method generates a random double value between 0 and 1.0.
C, public int nextint ()
The function of this method is to generate a random int value, which is between the int range, that is, between-231 and 231-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 in a specified range. The following describes how to generateCorresponding Interval. 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 Decimals in the [m, n) Range
Double D3 = R. nextdouble () * (N-m) + m;For example, a decimal number in the range [1, 2.5) is generated.
Double D3 = R. nextdouble () * 1.5 + 1;
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 ), then calculate the absolute value of the range, and the obtained range is.
Likewise,Generates random integers in any [0, n) range., You can use the following code:
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 [m, n) interval (note that the value must be greater than 1)
Int A = R. nextint (n-m + 1) + m;For example, an integer in the range [-3, 15) is generated.
Int N4 = R. nextint (18)-3;
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.
H,Probability implementation (generally 100 parts are divided into different probabilities)
Implementing 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 implement probability logic.
In the previous method introduction, the numbers generated by the nextint (int n) method are even, that is, the probability of each number in the interval being generated is the same. If a random integer in the [0,100) interval is generated, the probability of each digit is the same, and because the interval contains a total of 100 integers, therefore, the probability of each number is 1%. According to this theory, the probability problem in the program can be realized.
Example: A Random integer is generated. The integer is generated at a 55% probability of 1, 2 at a 40% probability, and 3 at a 5% probability. The implementation code is as follows:
Int N5 = R. nextint (100); int m; // result number if (N5 <55) {// The interval of 55 digits, the probability of 55% M = 1 ;} else if (N5 <95) {// [40%), 40 digit range, probability m = 2;} else {M = 3 ;}
Because the probability of each number is 1%, the interval probability of any 55 numbers is 55%. For the convenience of code writing, All integers in the [) range will be used here, and the Principles will be the same in the future.
Of course, the code here can be simplified, because the probability is a multiple of 5%, so you only need to control the probability based on 5%. below is the simplified code implementation:
int n6 = r.nextInt(20);int m1;if(n6 < 11){ m1 = 1;}else if(n6 < 19){ m1= 2; }else{ m1 = 3; }
In the program, the probability logic can be implemented according to the above description.
4. Other problems
A,Random objects with the same number of seeds (III)
Previously,For a random object with the same number of seeds, the random numbers generated by the same number of times are identical.The following is the test code:
Random r1 = new Random(10);Random r2 = new Random(10);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 the objects R1 and R2 is 10, and the random numbers generated by the two objects with the same number of times are identical.
To avoid the case where random numbers are the same, you must note that no matter how many random numbers need to be generated in the 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. The work of this random method is to generate a random decimal number in the [0, 1.0) range.
By reading the source code of the math class, we can find that the random method in the math class is implemented by directly calling the nextdouble method in the random class.
It's just that the call to the random method is relatively simple, so there are a lotProgrammers are used to using the random method of the math class to generate random numbers..