How and principle of random numbers in Java
Check the information related to random numbers and organize it specially
First, let’s talk about several ways to generate random numbers in java
EN. . . In fact, the third method above is used to generate random numbers in Random's default construction method.
There are two ways to build Random class in Method 2: with seeds and without seeds
Without seeds: This method will return random numbers, and the results of each run are different, which is equivalent to using System.currentTimeMillis() as the seed.
With seeds: In this way, the return result will be the same no matter how many times the program is run. 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
Random numbers in computers are pseudo-random numbers
Here is a C program like this:
// rand_1.cpp#include <stdlib.h>static unsigned int RAND_SEED;unsigned int random(void){ RAND_SEED = (RAND_SEED*123+59)%65536; return (RAND_SEED);}void random_start(void){ int temp[2]; movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4); RAND_SEED = temp[0];}void main(){ unsigned int i,n; random_start(); for(i=0;i<10;i++) printf("#u/t",random()); printf("/n");} It fully explains the process of random number generation:
first,
movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);
This function is used to move memory data. FP_SEG (far pointer to segment) is a function that takes the segment address of the temp array, FP_OFF (far pointer to offset) is a function that takes the relative address of the temp array, and the movedata function is to put the double words located in the 0040:006CH storage unit into the two memory 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.
Second,
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 it in Linux and Windows respectively. The random numbers generated by the same random seeds in these two operating systems are different, which means that they have different calculation methods.
Then,
movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);
Why do random seeds need to be retrieved at 0040:006CH in memory? What is stored at 0040:006CH?
Those who have studied the course "Principles of Computer Components and Interface Technology" may remember that when compiling the ROM BIOS clock interrupt service program, the Intel 8253 timing/counter is used. Its communication with the Intel 8259 interrupt chip enables the interrupt service program to operate. The 18.2 interrupts generated by the motherboard per second are generated by the processor controls the interrupt chip based on the timing/counter value. There is such a timing/counter on the motherboard of our computer to calculate the current system time. The counter will be added one every time a clock signal cycle passes. Where is the value of this counter stored? That's right, just at 0040:006CH in memory, this memory space is actually defined like this:
TIMER_LOW DW ?; Address is 0040:006CH
TIMER_HIGH DW ?; Address is 0040:006EH
TIMER_OFT DB ? ; Address is 0040:0070H
In the clock interrupt service program, whenever TIMER_LOW turns full, the counter will also turn full, and the value of the counter is zero, that is, the 16-bit binary at TIMER_LOW is zeroed, and TIMER_HIGH is added one. in rand01.c
movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);
It is precisely the two 16-bit binary numbers TIMER_LOW and TIMER_HIGH that are placed into the temp array and sent to RAND_SEED, thus obtaining the "random seed".
Now, one thing that can be determined is that the random seed comes from the system clock, or rather, the record value in memory from the timing/counter on the computer motherboard.
EN...No last. . lvl--
Let's look at another piece of code:
//rand_2.cpp#include <iostream>#include <cstdlib>using namespace std;int main(){ srand((unsigned)time(NULL)); unsigned int r=rand(); cout<<"r = "<<r<<endl; //According to the C++ 98 standard, the main function can be introduced without using the return statement to return 0;} Here, if the user and other programs do not set a random seed, the value of the system timing/counter is used as a random seed. Therefore, in the same platform environment, after compiling and generating an exe, each time you run it, the random number displayed will be a pseudo-random number, that is, the results displayed will be different every time you run it.
Summarize
Random numbers are values calculated by random seeds based on certain calculation methods. Therefore, as long as the calculation method is certain and the random seed is certain, the random number generated will not change. In the same platform environment, after compiling and generating an exe, the random numbers displayed are the same every time you run it. This is because in the same compilation platform environment, the calculation methods for generating random numbers from random seeds are the same, and the random seeds are the same, so the random numbers generated are the same.
As long as the user or third party does not set a random seed, then by default the random seed comes from the system clock (i.e. the value of the timing/counter)
Thank you for reading, I hope it can help you. Thank you for your support for this site!