search
HomeJavaJavaBaseHow to generate random numbers in java

How to generate random numbers in java

Jun 22, 2020 pm 05:58 PM
java

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.