Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways of generating random numbers in java

Shulou Source: shulou.com Published: 2022-06-02 11:50:28 09月15日 Update

This article mainly introduces the random number generation methods in java, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The generation of random numbers is very common in some code, and we must master it. There are three main ways to generate random numbers in java:

The first kind: new Random ()

The second kind: Math.random ()

The third kind: currentTimeMillis ()

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

The seed is to generate the first use value of the random number, the mechanism is that through a function, the value of the seed is transformed into a certain point in the random number space, and the resulting random number is evenly distributed in the space. The random number generated later is related to the previous random number. Take the code as an example.

Public static void main (String [] args) {Random r = new Random (1); for (int iTun0; I)

Tags: Random number seed method article different code common way generation function that is numerical value space type related two number value interest generator Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information Shulou Tech Info macOS OPPO Reno