The method of generating random numbers in Java and the principle detailed _java

Source: Internet
Author: User
Tags random seed

The generation and principle of random numbers in Java

Consult random number related data, special to do sorting

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

    1. In J2SE we can use the Math.random () method to produce a random number, the random number is a double between 0-1, we can multiply him by 100, he is a random number within 100, which is not in J2ME.
    2. In Java.util This package provides a random class, we can create a new random object to produce random numbers, he can produce random integers, random float, random double, random long, This is also a method that we often use in J2ME programs to take random numbers.
    3. In our system class, there is a Currenttimemillis () method that returns a number of milliseconds from January 1, 1970 0:0 0 to the present, the return type is long, we can take him as a random number, we can take him to some number modulo, You can limit him to a range.

En... In fact, the default construction method of random is to use the third method above to produce random numbers.

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

Without seed: This method will return a random number, each run the same result, the equivalent of using System.currenttimemillis () as the seed.

With seeds: 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 method call sequence is made for each instance, which generates and returns the same number sequence.

Pseudo random number

Random numbers in a computer are pseudo random numbers

Here's a C program that looks 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 describes the process of random number generation:

First of all

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

This function is used to move the memory data, where FP_SEG (far pointer to segment) is the function that takes the address of the temp array segment, and Fp_off (far to offset) is the function that takes the relative address of the temp array, The role of the Movedata function is to place the two words in the 0040:006CH storage unit in the two storage cells in the declaration of the array temp. This allows a 16-bit number at the 0040:006ch to be sent to rand_seed through the temp array.
Secondly

Rand_seed= (rand_seed*123+59)%65536;

is a method for calculating random numbers, and the method of calculating random numbers is different in different computers, even in different operating systems installed on the same computer. I've tried it on Linux and Windows, and the same random seed is different in terms of the random numbers generated in both operating systems, which means they're calculated differently.

And then

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

Why should random seeds be taken at the 0040:006ch of memory? What is stored in the 0040:006ch place?

The person who has studied the course "Computer composition principle and interface technology" may recall that the Intel 8253 Timing/counter was used when compiling the ROM BIOS Clock Interrupt service program, and its communication with Intel 8259 interrupt chip enabled the interrupt service program to operate. The 18.2 interrupts that the motherboard produces per second are the result of the processor controlling the interrupt chip based on the timer/register value. On our computer's motherboard there will be a timer/register to calculate the current system time, each clock signal cycle will make the register plus one, and the value of this register stored in where? Yes, in the memory of the 0040:006ch, in fact this section of memory space is so defined:

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 time, the register will also be full, the value of the register to zero, that is, timer_low at the 16-bit binary zero, and Timer_high plus one. In the rand01.c

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

It is the Timer_low and Timer_high two 16-bit binary numbers into the temp array, and then sent to the rand_seed, thus obtaining a "random seed."
Now, it is certain that the random seed comes from the system clock, specifically from the memory value of the timer/counter on the computer's motherboard.

En... No last. lvl--

Let's look at a 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, you can use the return statement to introduce the main function return
  0.
}

Here the user and other programs do not set random seeds, the value of the system Timer/counter is used as a random seed, so, in the same platform environment, after compiling the build exe, each time it is run, the random number displayed will be pseudo random number, that is, the results of each run display will be different.

Summarize

A random number is a numerical value calculated by a random seed based on a certain computational method. So, as long as the calculation method is certain, random seed is certain, then the random number that produces will not change. In the same platform environment, after compiling the build exe, each time it runs, the random number displayed is the same. this is because in the same compilation platform environment, random seed generation random number of calculation methods are the same, coupled with random seeds, so that the resulting random number is the same.

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 Timer/counter value)

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

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.