Several implementation methods of random number in Java

As we all know, random number is one of the most basic features of any programming language. The basic way to generate random numbers is the same: generate a random number between 0 and 1. It seems simple, but sometimes we ignore some interesting features.

What do we learn from books?

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

In all other languages, generating random numbers is like using math tool 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. Based on the above information, the developer will generate a double precision floating-point number between 0.0 and 10.0, which will be written as follows:

If an integer between 0 and 10 is generated, it will be written as:

Advanced

By reading math Random () source code, or simply use the automatic completion function of the IDE, which can be easily found by developers, Java lang.Math. Random () uses an internal randomly generated object - a powerful object that can be flexibly generated randomly: Boolean values, all number types, and even Gaussian distributions. For example:

It has a disadvantage that it is an object. Its method must be called through an instance, which means that its constructor must be called first. If the memory is sufficient, the above expression is acceptable; But when there is not enough memory, it will cause problems.

A simple solution can avoid creating a new instance every time you need to generate a random number, that is, using a static class. Guess you might think of Java Lang. math, good. We just improved Java Initialization of lang.math. Although the amount of work is low, you also have to do some simple unit tests to ensure that it will not make mistakes.

Assuming that the program needs to generate a random number to store, the problem comes again. For example, sometimes it is necessary to operate or protect the seed. An internal number is used to store the state and calculate the next random number. In these special cases, it is not appropriate to share randomly generated objects.

Concurrent

In the environment of Java EE multithreaded applications, randomly generated instance objects can still be stored in classes or other implementation classes as a static attribute. Fortunately, Java util. Random is thread safe, so there is no risk that multiple thread calls will destroy seeds.

Another thing worth considering is multithreaded Java An instance of lang.threadlocal. The lazy way is to implement a single instance through the Java API. Of course, you can also ensure that each thread has its own instance object.

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

This new API combines the advantages of two other methods: single instance / static access, like math As flexible as random (). Threadlocalrandom is also faster than any other method for handling high concurrency.

experience

Chris marasti Georg pointed out:

Make the distribution unbalanced, for example: 0.0 - 0.499999 will round to 0, and 0.5 to 1.499999 will round to 1. Then, how to use the old syntax to achieve the correct balanced distribution is as follows:

Fortunately, if we use Java util. Random or Java util. concurrent. Threadlocalrandom doesn't have to worry about the above problems.

Java actual combat project introduces some incorrect use of Java util. Harm of random API. This lesson tells us not to use:

Instead, use:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>