Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of generating random numbers by Java?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report