Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of generating random numbers by Java?

Shulou Source: shulou.com Published: 2022-06-02 20:05:02 09月21日 Update

This article introduces the knowledge of "what is the method of generating random numbers by Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are usually two ways to generate a random number within a specified range in Java:

Call the random () method of the Math class

Use the Random class.

The Random class provides a wealth of random number generation methods that can generate boolean, int, long, float, byte arrays, and random numbers of type double, which is the biggest difference between it and the random () method.

The random () method can only produce a random number of type 0x1 of type double.

The Random class is located in the java.util package, and there are two common constructors for this class.

1. Random (): this constructor uses a number corresponding to the current system time as the seed number, and then uses this seed number to construct the Random object.

2. Random (long seed): creates a new random number generator with a single parameter of type long.

The random numbers generated by all the methods provided by the Random class are evenly distributed, that is, the probability of generating numbers within the interval is equal. The following table lists the commonly used methods in the Random class.

The method shows that boolean nextBoolean () generates a random boolean value, and the probability of generating the values of true and false is equal to double nextDouble () to generate a random double value, which is between 0 but not 1.0int nextlnt () to generate a random int value, which is in the interval of int, that is,-231-1. If

If you need to generate the int value of the specified interval, you need to make a certain mathematical transformation int nextlnt (int n) to generate a random int value, which is between [0line n], containing 0 but not n. If you want to generate

The int value of the specified interval also requires a certain mathematical transformation void setSeed (long seed) to reset the number of seeds in the Random object. Random object after setting the seed count

Same long nextLong () returns a random long integer number boolean nextBoolean () returns a random Boolean value float nextFloat () returns a random floating point number double nextDouble () returns a random double value as the Random object created with the same seed number using the new keyword

The random () method of the Math class has no parameters, and it returns a double type random number greater than or equal to 0.0 and less than 1.0 by default, that is, 0

Tags: Methods generation random numbers numbers seeds intervals objects types same that is content parameters common use mathematics more probability knowledge range transformation different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno MariaDB Redmi Apple