JAVA knowledge about cold Random class (from rookie V) JAVA's Random class (rpm)

Random class of JAVA (rpm)

Random class (Classes in java.util)         

Random class random algorithm is implemented in pseudo-random, that is, regular random. When performing a random, random numbers originated algorithm called seed number (SEED), a certain number of seeds basis transform, thereby generating a random number required.

The same number of seed Random object, the same number of random numbers generated are identical. That is, the same number of seed Random object two, to generate the first random number are identical, the second random number generated is exactly the same. This requires special attention when generating a plurality of random numbers.

Here are some of the Random class use, and how to generate random chance of an array of the specified range and the achievement of program requirements.

1, the object to generate Random

         Random class contains two constructors, the following are introduced successively:

         a、public Random()

This configuration method uses a relative time and the current system time corresponding to the relevant figures, as the number of seeds, and the seeds configured using Random object.

         b、public Random(long seed)

The constructor can be created through the development of a seed number.

Sample code:

Random r = new Random();

Random r1 = new Random(10);

Again: Origins Digital seeds just random algorithm, and generated random numbers interval is independent.

2, Random class common method

Random class method is relatively simple, the function of each method is also very easy to understand. Incidentally, Random each class method for generating random numbers are uniformly distributed, the probability of that is generated by the internal digital section are equal. Next, these methods do some basic introduction:

a、public boolean nextBoolean()

The effect of the method is to generate a random boolean value, generating equal probability of true and false values, i.e. is a 50% chance.

b、public double nextDouble()

The effect of the method is to generate a random double value, values ​​between [0,1.0).

 

c nextInt public int ()

 

The effect of the method is to generate a random int value between int interval, i.e. between -231 to 231-1.

If you need to generate int specifying section, the need for some mathematical transformation, specifically with reference to the code used in the following example.

d, public int nextInt (int n)

The effect of the method is to generate a random int value range [0, n-) interval, i.e. int random value between 0 and n, not including 0 contains n.

If you want to generate an int specified range, but also requires some mathematical transformation, specifically with reference to the code used in the following example.

e、public void setSeed(long seed)

The role of this method is to re-set the number of seed Random object. After setting up the same number of seed Random object and the same number of seeds using the new keyword to create the Random object.

3, Random class uses examples

Random class used, generally generate random numbers specified range, the following eleven describes how to generate random numbers corresponding to the section. The following codes generated random number r are performed using the Random object to generate:

Random r = new Random();

a, generating [0,1.0) fractional interval

                   double d1 = r.nextDouble();

Directly nextDouble method available.

b, to generate [0,5.0) fractional interval

double d2 = r.nextDouble() * 5;

Since nextDouble method for generating a digital interval is [0,1.0), to expand the range that is 5 times the required interval.

 

Similarly, to generate [0, random decimal d) interval, d is an arbitrary positive decimal, you only need to multiply the return value of d can nextDouble method.

c, generating [1,2.5) fractional interval

       double d3 = r.nextDouble() * 1.5 + 1;

Generating [1,2.5) random fractional interval, only need to generate [0, 1.5) random number interval, then the generated random number plus 1 interval can.

Similarly, any non generated from the fractional interval [d1, random number d2) in the range of zero (wherein d1 is not equal to 0), then only need to generate [0, d2-d1) random number interval, and then the resultant random number can range plus d1.

d, generate an arbitrary integer

int n1 = r.nextInt();

NextInt method can be used directly.

e, generating an integer [0,10) section

int n2 = r.nextInt(10);

n2 = Math.abs(r.nextInt() % 10);

Two or more lines of code can be generated integer [0,10) interval.

The first implementation uses Random class nextInt (int n) direct implementation method.

A second implementation, the nextInt first call () int method generates an arbitrary number, and the number after the formation of module 10 of the digital interval (-10,10), then the absolute value of the range, the resulting that is, the interval [0, 10) a.

Similarly, to generate an arbitrary [0, n) random integer interval, you can use the following code:

int n2 = r.nextInt(n);

n2 = Math.abs(r.nextInt() % n);

f, to generate an integer of [0,10] interval

int n3 = r.nextInt(11);

n3 = Math.abs(r.nextInt() % 11);

With respect to the integer interval [0, 10] and the interval [0,11) equivalent interval, i.e., it generates an integer [0,11) interval.

g, generate integer [-3,15) section

int n4 = r.nextInt(18) - 3;

n4 = Math.abs(r.nextInt() % 18) - 3;

Generating a non-random integer from the interval 0 starts, with reference to the above description can achieve the principles of the non-zero sections of the decimal.

h, the chance to achieve

Implement the program logic according to certain random chance is dealing with a problem can be solved. Below a simple example demonstrates how to use random numbers to realize the logic of probability.

In the method described in the foregoing, nextInt (int n) generated digital method is uniform, that is to say the probability of each number generated inside this interval are identical. If it generates a [0,100) section random integer, then the probability of each number generated should be the same, but since this interval a total of 100 integers, each digit is a 1% chance. According to this theory, the probability of problems in the program can be achieved.

Example: generating a random integer generated a probability of 55%, a 40% chance to generate 2 to 5% chance of generating 3. Code is implemented as follows:

int n5 = r.nextInt(100);

int m; // result of the digital

if (n5 <55) {// 55-digit interval, 55% probability

 

m = 1;

} Else if (n5 <95) {// [55,95), 40-digit interval, a 40% chance

m = 2;

}else{

m = 3;

}

Because the probability of each digit is a 1% chance of the digital section 55 is arbitrary is 55%, in order to facilitate the writing of code, as used herein, all integers [0,55) interval, as follow-up principle.

Of course, where the code can be simplified, because the probability is a multiple of 5%, so long as the 5% probability can be controlled on the basis, the following is a simplified code implements:

         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 can be implemented in the logic described above.

4, other issues

a, the same number of seed Random object problem

Recall that the same number of seed Random object, the same number of random numbers generated are identical, the following 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 seed object r1 and r2 are used for 10, then the two of the same object number generated random number are identical.

If you want to avoid the same random numbers happens, you need to pay attention to, no matter how many items you need to generate random numbers, can only use a Random object.

b, random method on Math class

In fact, there are a random method Math class, the working method is to generate a random [0,1.0) random fractional interval.

Math class can be found by reading the source code, random method of the Math class is a direct method calls nextDouble Random class implementation.

Just call random method is relatively simple, so a lot of programmers are used to generate random numbers using random method of the Math class.

Random class (Classes in java.util)         

Random class random algorithm is implemented in pseudo-random, that is, regular random. When performing a random, random numbers originated algorithm called seed number (SEED), a certain number of seeds basis transform, thereby generating a random number required.

The same number of seed Random object, the same number of random numbers generated are identical. That is, the same number of seed Random object two, to generate the first random number are identical, the second random number generated is exactly the same. This requires special attention when generating a plurality of random numbers.

Here are some of the Random class use, and how to generate random chance of an array of the specified range and the achievement of program requirements.

1, the object to generate Random

         Random class contains two constructors, the following are introduced successively:

         a、public Random()

This configuration method uses a relative time and the current system time corresponding to the relevant figures, as the number of seeds, and the seeds configured using Random object.

         b、public Random(long seed)

The constructor can be created through the development of a seed number.

Sample code:

Random r = new Random();

Random r1 = new Random(10);

Again: Origins Digital seeds just random algorithm, and generated random numbers interval is independent.

2, Random class common method

Random class method is relatively simple, the function of each method is also very easy to understand. Incidentally, Random each class method for generating random numbers are uniformly distributed, the probability of that is generated by the internal digital section are equal. Next, these methods do some basic introduction:

a、public boolean nextBoolean()

The effect of the method is to generate a random boolean value, generating equal probability of true and false values, i.e. is a 50% chance.

b、public double nextDouble()

The effect of the method is to generate a random double value, values ​​between [0,1.0).

 

c nextInt public int ()

 

The effect of the method is to generate a random int value between int interval, i.e. between -231 to 231-1.

If you need to generate int specifying section, the need for some mathematical transformation, specifically with reference to the code used in the following example.

d, public int nextInt (int n)

The effect of the method is to generate a random int value range [0, n-) interval, i.e. int random value between 0 and n, not including 0 contains n.

If you want to generate an int specified range, but also requires some mathematical transformation, specifically with reference to the code used in the following example.

e、public void setSeed(long seed)

The role of this method is to re-set the number of seed Random object. After setting up the same number of seed Random object and the same number of seeds using the new keyword to create the Random object.

3, Random class uses examples

Random class used, generally generate random numbers specified range, the following eleven describes how to generate random numbers corresponding to the section. The following codes generated random number r are performed using the Random object to generate:

Random r = new Random();

a, generating [0,1.0) fractional interval

                   double d1 = r.nextDouble();

Directly nextDouble method available.

b, to generate [0,5.0) fractional interval

double d2 = r.nextDouble() * 5;

Since nextDouble method for generating a digital interval is [0,1.0), to expand the range that is 5 times the required interval.

 

Similarly, to generate [0, random decimal d) interval, d is an arbitrary positive decimal, you only need to multiply the return value of d can nextDouble method.

c, generating [1,2.5) fractional interval

       double d3 = r.nextDouble() * 1.5 + 1;

Generating [1,2.5) random fractional interval, only need to generate [0, 1.5) random number interval, then the generated random number plus 1 interval can.

Similarly, any non generated from the fractional interval [d1, random number d2) in the range of zero (wherein d1 is not equal to 0), then only need to generate [0, d2-d1) random number interval, and then the resultant random number can range plus d1.

d, generate an arbitrary integer

int n1 = r.nextInt();

NextInt method can be used directly.

e, generating an integer [0,10) section

int n2 = r.nextInt(10);

n2 = Math.abs(r.nextInt() % 10);

Two or more lines of code can be generated integer [0,10) interval.

The first implementation uses Random class nextInt (int n) direct implementation method.

A second implementation, the nextInt first call () int method generates an arbitrary number, and the number after the formation of module 10 of the digital interval (-10,10), then the absolute value of the range, the resulting that is, the interval [0, 10) a.

Similarly, to generate an arbitrary [0, n) random integer interval, you can use the following code:

int n2 = r.nextInt(n);

n2 = Math.abs(r.nextInt() % n);

f, to generate an integer of [0,10] interval

int n3 = r.nextInt(11);

n3 = Math.abs(r.nextInt() % 11);

With respect to the integer interval [0, 10] and the interval [0,11) equivalent interval, i.e., it generates an integer [0,11) interval.

g, generate integer [-3,15) section

int n4 = r.nextInt(18) - 3;

n4 = Math.abs(r.nextInt() % 18) - 3;

Generating a non-random integer from the interval 0 starts, with reference to the above description can achieve the principles of the non-zero sections of the decimal.

h, the chance to achieve

Implement the program logic according to certain random chance is dealing with a problem can be solved. Below a simple example demonstrates how to use random numbers to realize the logic of probability.

In the method described in the foregoing, nextInt (int n) generated digital method is uniform, that is to say the probability of each number generated inside this interval are identical. If it generates a [0,100) section random integer, then the probability of each number generated should be the same, but since this interval a total of 100 integers, each digit is a 1% chance. According to this theory, the probability of problems in the program can be achieved.

Example: generating a random integer generated a probability of 55%, a 40% chance to generate 2 to 5% chance of generating 3. Code is implemented as follows:

int n5 = r.nextInt(100);

int m; // result of the digital

if (n5 <55) {// 55-digit interval, 55% probability

 

m = 1;

} Else if (n5 <95) {// [55,95), 40-digit interval, a 40% chance

m = 2;

}else{

m = 3;

}

Because the probability of each digit is a 1% chance of the digital section 55 is arbitrary is 55%, in order to facilitate the writing of code, as used herein, all integers [0,55) interval, as follow-up principle.

Of course, where the code can be simplified, because the probability is a multiple of 5%, so long as the 5% probability can be controlled on the basis, the following is a simplified code implements:

         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 can be implemented in the logic described above.

4, other issues

a, the same number of seed Random object problem

Recall that the same number of seed Random object, the same number of random numbers generated are identical, the following 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 seed object r1 and r2 are used for 10, then the two of the same object number generated random number are identical.

If you want to avoid the same random numbers happens, you need to pay attention to, no matter how many items you need to generate random numbers, can only use a Random object.

b, random method on Math class

In fact, there are a random method Math class, the working method is to generate a random [0,1.0) random fractional interval.

Math class can be found by reading the source code, random method of the Math class is a direct method calls nextDouble Random class implementation.

Just call random method is relatively simple, so a lot of programmers are used to generate random numbers using random method of the Math class.

Guess you like

Origin www.cnblogs.com/Dmand/p/11915455.html