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:
Random r = new Random ();
Random r1 = new Random (10);
Again, the seed number is only the origin number of the stochastic algorithm, regardless of the interval of the generated random numbers.
Validation: Random objects of the same seed number, the same number of times generated are exactly the same.
ImportJava.util.Random; Public classTest { Public voidrandom () {intI=0; intJ=0; Random Random=NewRandom (1); Random random1=NewRandom (1); I=Random.nextint (); J=Random1.nextint (); System.out.println (i+"-----"+j); } Public Static voidMain (string[] args) {Test TT=Newtest (); Tt.random (); }}
Results:
First time:
Modify the origin number so that it equals 10.
Random random=New random (ten); Random random1=new random (10);
Observation results:
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 (): 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 (): is to generate a random double value between [0,1.0].
c, public int nextint (): is the int value generated between -2^31 to 2^31-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): is to generate an interval int value between [0,n] , containing 0 without n. If you want to generate a specified interval int value, you also need to do some mathematical transformations, see the code in the following example.
e, public void setseed (long seed): is to re-set 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.
f, public float nextfloat (int n): returns the next pseudo-random number, which is a 0.0
float value that is evenly distributed between and 1.0
between this random number generator sequence.
g, public Long Nextlong (): Returns the next pseudo-random number, which is a long value derived from the uniform distribution of this random number generator sequence.
h, public Double Nextgaussian (): Returns the next pseudo-random number, which is a double value of Gaussian ("normal") distribution taken from this random number generator sequence, with an average of 0.0 and a standard deviation of 1.0.
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 expansion is 5 times times 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
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 ( -2^31 to 2^31-1)
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 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:
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;
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.
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:
int n5 = r.nextint (100);
int m; Result numbers
if (N5 < 55) {//55-digit interval, 55% chance
m = 1;
}else if (N5 < 95) {//[55,95), 40-digit interval, 40% chance
m = 2;
}else{
m = 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:
int N6 = R.nextint (20);
int M1;
if (N6 < 11) {
m1 = 1;
}else if (N6 < 19) {
M1= 2;
}else{
m1 = 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:
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 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.
Introduction to the Random class in Java