The generation and principle of random numbers in Java

Source: Internet
Author: User
Tags random seed

Review random number related data, special do a few ways to generate random numbers in Java first.
    1. In J2SE we can use the Math.random () method to generate a random number, which is a double between 0-1 and we can multiply it by 100, which is a random number within 100, which is not in J2ME.
    2. In Java.util, a random class is provided in this package, and we can create a random object to generate a stochastic number, which can be produced by randomly integer, random float, random double, random long, This is also a method that we often use to take random numbers in the J2ME program.
    3. In our system class there is a Currenttimemillis () method, which returns a number of milliseconds from January 1, 1970 0:0 0 seconds to the current one, the return type is long, we can take him as a random number, we can take him to some number modulo, You can confine him to a range.

En... In fact, in the default construction method of random, the third method is used to generate random numbers.

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

  • Without seeds: This method will return random numbers, each running a different result, equivalent to using System.currenttimemillis () as the seed.
  • With seed: This way, no matter how many times the program runs, the result is the same. If you create two random instances with the same seed, the same method call sequence is made for each instance, and they will generate and return the same sequence of numbers.
Pseudo-Random number

Random numbers in a computer are pseudo-random numbers

See Below a 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 completely illustrates the process of generating random numbers:
First of all

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);
这个函数用来移动内存数据,其中FP_SEG(far pointer to segment)是取temp数组段地址的函数,FP_OFF(far pointer to offset)是取temp数组相对地址的函数,movedata函数的作用是把位于0040:006CH存储单元中的双字放到数组temp的声明的两个存储单元中。这样可以通过temp数组把0040:006CH处的一个16位的数送给RAND_SEED。

Secondly

RAND_SEED=(RAND_SEED*123+59)%65536;
是用来计算随机数的方法,随机数的计算方法在不同的计算机中是不同的,即使在相同的计算机中安装的不同的操作系统中也是不同的。我在linux和windows下分别试过,相同的随机种子在这两种操作系统中生成的随机数是不同的,这说明它们的计算方法不同。

And then

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);
随机种子为什么要在内存的0040:006CH处取?0040:006CH处存放的是什么?

The person who has studied the computer composition principle and interface technology may remember that the Intel 8253 Timer/counter is used when compiling the ROM BIOS Clock Interrupt service program, and its communication with the Intel 8259 interrupt chip allows the interrupt service program to function. The 18.2 interrupts generated per second by the motherboard are generated by the processor based on the timer/Register value control interrupt chip. On the motherboard of our computer there is a timer/register to calculate the current system time, each clock signal cycle will make the register to add one, and the value of this register is stored where? Yes, in the memory of the 0040:006ch, in fact, this section of memory space is defined as:

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 the Timer_low is full, at this point, the register will also be full, the value of the register is zero, that is, the timer_low at the 16-bit binary zero, and Timer_high plus one. Rand01.c in the

Movedata (0x0040,0x006c,fp_seg (temp), Fp_off (temp), 4);

The Timer_low and Timer_high two 16-bit binary numbers were put into the temp array and sent to Rand_seed, resulting in "random seed".
Now, one thing to be sure is that the random seed comes from the system clock and, to be precise, is the value of the Timer/counter in memory from the computer's motherboard.

En... No last. lvl--
Look at the code again:

//rand_2.cpp#include <iostream>#include <cstdlib>using namespace std;int main(){    srand((unsigned)time(NULL));    unsigned int r=rand();    cout<<"r = "<<r<<endl; //根据C++ 98标准,可以不用return语句来介绍main函数    return 0;}
这里用户和其他程序没有设定随机种子,则使用系统定时/计数器的值做为随机种子,所以,在相同的平台环境下,编译生成exe后,每次运行它,显示的随机数会是伪随机数,即每次运行显示的结果会有不同。
Summarize
    • A random number is a numerical value calculated by a random seed based on a certain calculation method. Therefore, as long as the calculation method must be, random seed must, then the resulting random number will not change. in the same platform environment, after compiling the EXE, each time it is run, the random number displayed is the same. This is because, in the same compilation platform environment, the random numbers generated by random seeds are calculated in the same way, with random seeds, so the random number generated is the same.

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

The generation and principle of random numbers in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.