Home >Java >JavaBase >How to generate random numbers in java

How to generate random numbers in java

hzc
hzcOriginal
2020-06-22 17:58:225405browse

How to generate random numbers in java The generation of random numbers is very commonly used in some codes, and it is also something we must master. There are three main ways to generate random numbers in java:

The first one: new Random()

The second one: Math.random()

The third type: currentTimeMillis()

The first type requires the use of the java.util.Random class to generate a random number Generator is also the most commonly used one. It has two constructors, Random() and Random(long seed). The first one uses the current time as the default seed, and the second one uses the specified seed value. After generation, different types of numbers are generated with the help of different statements.

The seed is the first value used to generate a random number. The mechanism is to use a function to convert the value of this seed into a certain point in the random number space, and the generated random numbers are evenly distributed in space. The random numbers generated in the future are related to the previous random number. Take code as an example.

public static void main(String[] args)
{
  Random r = new Random(1);
  for(int i=0 ; i<5 ;  i++)
  {
    int ran1 = r.nextInt(100);
    System.out.println(ran1);
  }
}

The five numbers generated by my compiler are all 85,88,47,13,54. If Random r = new Random() is used, the random numbers generated will be different. This is determined The result of the seed.

The value returned by the second method is a double value of [0.0,1.0). Since the double type number has high precision, it can be regarded as a random number to a certain extent and can be carried out with the help of (int) Type conversion can get integer random numbers, the code is as follows.

  public static void main(String[] args)     {    
        int max=100,min=1;      
        int ran2 = (int) (Math.random()*(max-min)+min); 
        System.out.println(ran2);   
   }

As for the third method, although it is not commonly used, it is also an idea. The method returns a long number of milliseconds from 0:00:00 on January 1, 1970 (this is related to UNIX systems) to the present. After taking the modulo, a random number within the required range can be obtained.

public static void main(String[] args)
    {
        int max=100,min=1;
        long randomNum = System.currentTimeMillis();
        int ran3 = (int) (randomNum%(max-min)+min);
        System.out.println(ran3);

    }

Recommended tutorial: "java tutorial"

The above is the detailed content of How to generate random numbers in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn