Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of generating java Random number

Shulou Source: shulou.com Published: 2022-05-31 17:49:49 10月25日 Update

In this article Xiaobian for you to introduce in detail the "java random number generation method", the content is detailed, the steps are clear, the details are handled properly, I hope this "java random number generation method" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer.

1. Random generating number method

Final double v = Math.random ()

Or

Final int i = new Random () .nextInt (int bound)

2. Principle analysis

(1) static (static) method random () in Math library

The purpose of this method is to produce a multiplier value of 0 to 1 (including 0, but not 1).

(2) use Random class objects.

The program can generate many different types of random numbers by calling nextInt () and nextFloat () (or calling nextLong () or nextDouble (). The parameter passed to nextInt () sets the upper limit of the random number and the lower limit of 0.

3. Examples

/ / Constructor (1): create a new random number generator. Random () / / constructor (2): use a single long seed to create a new random number generator: public Random (long seed) {setSeed (seed);} the next method uses it to save the state of the random number generator. Random (long seed) boolean nextBoolean () / / returns the next pseudo-random number of "boolean type". Void nextBytes (byte [] buf) / / generates random bytes and places them in the byte array buf. Double nextDouble () / / returns a random number of "double types between [0.0,1.0)". Float nextFloat () / / returns a random number of "float types between [0.0,1.0)". Int nextInt () / / returns the next "int type" random number. Int nextInt (int n) / / returns a random number of int types between [0, n). Long nextLong () / / returns the next "long type" random number. Synchronized double nextGaussian () / / returns the next random number of "double type", which is a double value with a Gaussian ("normal") distribution, with an average of 0.0 and a standard deviation of 1.0. Synchronized void setSeed (long seed) / / sets the seed for this random number generator with a single long seed. After reading this, the article "the method of generating java random numbers" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Tags: Random number generation type method generator inter article seed content function single byte different proper upper limit lower limit action deviation principle parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux MySQL Apple Shulou Tech Info