Talk about n random number generation methods in Java

  • 2020-04-01 04:10:48
  • OfStack

What do we learn from books?

The most obvious and intuitive way to generate random Numbers in Java is to simply call:


java.lang.Math.random() 

In all other languages, generating random Numbers is like using Math utility classes such as abs, pow, floor, SQRT, and other mathematical functions. Most people learn about this class through books, tutorials, and courses. A simple example: a double-precision floating point number can be generated from 0.0 to 1.0. So with the above information, the developer is going to produce a double-precision floating point between 0.0 and 10.0 and will write:


Math.random() * 10 

An integer between 0 and 10 is written as:


Math.round(Math.random() * 10) 

In   order

By reading the source code for math.random (), or simply taking advantage of the IDE's auto-complete capabilities, developers can easily see that java.lang.math.random () USES an internal random-generated object -- a powerful object that can be generated flexibly at random: Boolean values, all number types, even gaussian distributions. Such as:


new java.util.Random().nextInt(10) 

It has the disadvantage that it is an object. Its methods must be called through an instance, which means its constructor must be called first. If memory is sufficient, the above expression is acceptable. But when you run out of memory, you have a problem.

A simple solution that avoids the need to create a new instance every time a random number is generated is to use a static class. Guess you might think of java.lang.math, good, we just improved the initialization of java.lang.math. Although this is a low volume project, you should also do some simple unit tests to make sure it doesn't go wrong.

Assuming the program needs to generate a random number to store, the problem arises again. For example, sometimes you need to manipulate or protect a seed, an internal number to store state and calculate the next random number. In these special cases, sharing randomly generated objects is not appropriate.

And   hair

In the context of a Java EE multithreaded application, a randomly generated instance object can still be stored in a class or other implementation class as a static property. Fortunately, java.util.Random is thread-safe, so there is no risk that multiple thread calls will break the seed.

Another example worth considering is an instance of multithreaded java.lang.threadlocal. The lazy way is to implement singletons through the Java API itself, and of course you can make sure that each thread has its own instance object.

Although Java does not provide a good way to manage a single instance of java.util. Random. However, the long-awaited Java 7 release provides a new way to generate random Numbers:


java.util.concurrent.ThreadLocalRandom.current().nextInt(10)

This new API combines the advantages of the other two approaches: single instance/static access, which is as flexible as math.random (). ThreadLocalRandom is also faster than any other method for dealing with high concurrency.
experience

Chris marasti-georg points out:


Math.round(Math.random() * 10) 

Make the distribution unbalanced, for example: 0.0-0.499999 rounds to 0, and 0.5 to 1.499999 rounds to 1. So how to use the old syntax to achieve the correct equilibrium distribution, as follows:


Math.floor(Math.random() * 11 )  

Fortunately, if we use the Java util. Random or Java. Util. Concurrent. ThreadLocalRandom do not have to worry about these problems.

The Java live project introduces some of the hazards of using the java.util.Random API incorrectly. This lesson teaches us not to use:


Math.abs(rnd.nextInt())%n 

To use:


rnd.nextInt(n) 

Y above is about the introduction of n random Numbers in Java, I hope to help you.


Related articles: