Detailed explanation of the generation mode and principle of random numbers in Java

Generation mode and principle of random number in Java

Consult the data related to random numbers and sort them out

First, let's talk about several ways to generate random numbers in Java

EN。。。 In fact, the third method above is also used to generate random numbers in random's default construction method.

There are two ways to build the random class in method 2: with seed and without seed

No seed: random numbers will be returned in this way. The results will be different each time. It is equivalent to using system Currenttimemillis() as seed.

With seed: in this way, no matter how many times the program runs, the return result is the same. If two random instances are created with the same seed, the same sequence of method calls is made to each instance, and they will generate and return the same sequence of numbers.

Pseudo random number

All random numbers in a computer are pseudo-random numbers

Here is a C program:

It completely describes the process of generating random numbers:

first,

movedata(0x0040,4);

This function is used to move memory data, where FP_ SEG (far pointer to segment) is a function that takes the address of the temp array segment, fp_off (far pointer to offset) is a function to get the relative address of the temp array. The movedata function is used to put the doubleword in the 0040:006ch storage unit into the two storage units declared by the array temp. In this way, a 16 bit number at 0040:006ch can be sent to rand_seed through the temp array. Secondly,

RAND_ SEED=(RAND_SEED*123+59)%65536;

It is a method used to calculate random numbers. The calculation method of random numbers is different in different computers, even in different operating systems installed in the same computer. I have tried under Linux and windows respectively. The same random seed generates different random numbers in these two operating systems, which shows that their calculation methods are different.

then,

movedata(0x0040,4);

Why should random seeds be fetched at 0040:006ch in memory? 0040:006ch what is stored?

Those who have studied computer composition principle and interface technology may remember that Intel 8253 timer / counter is used when compiling ROM BIOS clock interrupt service program. Its communication with Intel 8259 interrupt chip makes the interrupt service program run. The 18.2 interrupts generated by the motherboard per second are generated by the processor controlling the interrupt chip according to the timer / counter value. On the motherboard of our computer, there will be such a timer / counter to calculate the current system time. Every clock signal cycle will increase the counter by one, and where is the value of this counter stored? Yes, at 0040:006ch of the memory, in fact, this memory space is defined as follows:

TIMER_ LOW DW ? ; The address is 0040:006ch timer_ HIGH DW ? ; The address is 0040:006eh timer_ OFT DB ? ; The address is 0040:0070h

In the clock interrupt service program, whenever timer_ When low turns full, the counter will also turn full, and the value of the counter will return to zero, that is, timer_ 16 bit binary zeroing at low and timer_ High plus one. rand01. In C

movedata(0x0040,4);

It's the timer_ Low and timer_ High two 16 bit binary numbers are put into the temp array and sent to RAND_ Seed, thus obtaining a "random seed". Now, it can be determined that the random seed comes from the system clock, specifically, the recording value in memory of the timing / counter on the computer motherboard.

EN... No final.. lvl--

Look at another code:

Here, users and other programs do not set random seed, so the value of system timing / counter is used as random seed. Therefore, in the same platform environment, after compiling and generating exe, the random number displayed each time it is run will be pseudo-random number, that is, the results displayed each time will be different.

summary

Random number is the value calculated by random seed according to a certain calculation method. Therefore, as long as the calculation method is certain and the random seed is certain, the generated random number will not change. In the same platform environment, after compiling and generating exe, the random number displayed is the same every time it is run. This is because in the same compilation platform environment, the calculation method of generating random numbers from random seeds is the same. In addition, the random seeds are the same, so the generated random numbers are the same.

As long as the user or third party does not set the random seed, the random seed comes from the system clock (i.e. the value of timing / counter) by default

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>