The method and principle of generating random Numbers in Java

  • 2020-05-17 05:27:28
  • OfStack

The generation method and principle of random Numbers in Java

Consult the random number related data, do the special arrangement

First of all, let's talk about the ways to generate random Numbers in java

In j2se we can use the Math.random () method to generate a random number, which is an double between 0 and 1, and we can multiply it by 100, which is a random number within 100, which is not found in j2me. The java.util package provides a class of Random, we can create a new Random object to generate random Numbers, it can produce random integer, random float, random double, random long, this is also we often use in the j2me program a random number method. In our System class, we have an currentTimeMillis() method that returns the number of milliseconds from January 1, 1970, 0:0:0 to the current number of milliseconds. The return type is long. We can take it as a random number.

EN... In fact, the default Random constructor also USES the third method above to generate random Numbers.

There are two ways to build the Random class in method 2: with and without seeds

No seed: this will return a random number, with different results per run, equivalent to System.currentTimeMillis () as the seed.

Seeded: this way, no matter how many times the program is run, the return result is the same. If two instances of Random are created from the same seed, the same sequence of method calls is made for each instance, and they will generate and return the same sequence of Numbers.

Pseudo random number

All random Numbers in a computer are pseudorandom

Here's an C program:


// 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 of all,

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);

FP_SEG (far pointer to segment) is a function that takes the relative address of temp array. FP_OFF (far pointer to offset) is a function that takes the relative address of temp array. movedata is a function that puts the double word in the 0040:006CH storage unit into the two storage units declared by temp array. This allows you to send a 16-bit number from 004:006CH to RAND_SEED through the temp array.
Second,

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

Is used to calculate the method of random number, the calculation method of random number is different in different computer, even in the same computer installed in different operating system is also different. I have tried linux and windows respectively, and the same random seed generates different random Numbers in the two operating systems, which indicates that they are calculated in different ways.

And then,

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);

Why do random seeds need to be fetched in memory at 0040:006CH? What is stored at 0040:006CH?

Studied the computer composition principle and interface technology "this course may remember in the preparation of ROM BIOS clock interrupt service routine will use Intel 8253 timer/counter, its communications with Intel 8259 interrupt chip makes the interrupt service routine work and motherboard of interrupt is 18.2 times per second processor according to the value of timer/counter interrupt control chip. There will be such a timing/counter on the mainframe board of our computer to calculate the current system time. Every time a clock signal cycle passes, it will cause the counter to add 1. Where does the value of this counter reside? That's right, it's at 0040:006CH, and this is how it's defined:

TIMER_LOW DW & # 63; ; Address is: 0040 006 CH
TIMER_HIGH DW & # 63; ; Address is: 0040 006 EH
TIMER_OFT DB & # 63; ; Address is: 0040 0070 H

In the clock interrupt service program, whenever TIMER_LOW rolls over, then the counter will also roll over, and the value of counter will return to zero, that is, the 16-bit 2-base system at TIMER_LOW will return to zero, while TIMER_HIGH will add 1. rand01. The c

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);

It is by putting TIMER_LOW and TIMER_HIGH in the temp array and sending them to RAND_SEED that the "random seed" is obtained.
Now, one thing you can be sure of is that the random seed comes from the system clock, or rather, from the time/counter in memory on the computer's motherboard.

EN... There is no end. lvl -

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 , Don't have to return Statement to introduce main function 
  return 0;
}

In this case, the user and other programs do not set the random seed, so the system timing/counter value is used as the random seed. Therefore, in the same platform environment, after compiling and generating exe, the random number displayed will be pseudorandom every time it is run, that is, the result displayed will be different every time it is run.

conclusion

A random number is a numerical value calculated by a random seed according to a 1-definite calculation method. Therefore, as long as the calculation method is 1 definite and the random seed is 1 definite, the random number generated will not change. In the same platform environment, after the compilation generates exe, the random number displayed is 1 every time you run it. This is because in the same compiler platform environment, the calculation method of generating random Numbers from the random seed is 1, plus the random seed 1, so the generated random Numbers are 1.

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

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


Related articles: