Some understandings of random functions in java. In java, random functions are interpreted as pseudo-random number generators because random number generation is related to seed seeds, as long as the random number generated by the same seed is similar to the random function in java
Random () is a pseudo-random number generator, because its random number generation is related to the seed. as long as the random number generated by the same seed is the same, no seed is specified.
A seed parameter is generated based on the current time.
In a blog, you can see this sentence. In fact, Random is a pseudo-Random number. the same seed produces the same sequence (note that it is not the same value ).
Why do we generate the same sequence? here we need to understand how the random number is generated after the Constructor sets the seed (whether it is set by itself or by default:
First, the nextInt () function will generate a random value based on the current seed, then assign the generated random number to seed, and continue to generate a random number.
So understand the following two sections of code.
- public static void main(String[] args) {
- for (int i=0;i<10;i++){
- Random r = new Random(10);
- data = r.nextInt(10);
- System.out.print(rand);
- System.out.print(" ");
- }
-
- }
The 10 numbers generated are the same.
- public static void main(String[] args) {
- Random r = new Random(10);
- for (int i=0;i<10;i++){
- data = r.nextInt(10);
- System.out.print(rand);
- System.out.print(" ");
- }
- }
The 10 numbers generated by the above code are different, but the 10 numbers generated during re-execution are the same as those generated by the previous for loop execution. this is what the above sentence means.
By default, random () generates seeds in the same way. However, if you replace the preceding values with random (), the random numbers generated after multiple for loops are different, because the time for each execution is different, the default seed is also different. we can see that the key to generating random numbers is to have a truly random seed.
Random () is a pseudo-random number generator, because its random number generation is related to the seed, as long as the random number generated by the same seed is all...