Home >Java >javaTutorial >Several interesting uses of Java random numbers

Several interesting uses of Java random numbers

伊谢尔伦
伊谢尔伦Original
2016-11-26 09:16:221637browse

 As we all know, random numbers are 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 overlook some interesting features.

Several interesting uses of Java random numbers

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 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. Then based on the above information, if a developer wants to generate a double-precision floating point number between 0.0 and 10.0, he will write it like this:

Math.random() * 10

 And to generate an integer between 0 and 10, he will write it like this:

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

 Advanced

 Pass By reading the source code of Math.random(), or simply taking advantage of the IDE's autocomplete feature, developers can easily discover that java.lang.Math.random() uses an internally generated randomly generated object – a very powerful object that can be used flexibly Randomly generated: Boolean values, all numeric types, even Gaussian distribution. For example:

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

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

 A simple solution to avoid creating a new instance every time you need to generate a random number is to use a static class. I guess you may have thought of java.lang.Math. Very good. We just improved the initialization of java.lang.Math. Although this project is low-volume, you should also do some simple unit tests to ensure that it does not go wrong.

  Assuming that the program needs to generate a random number for storage, the problem arises again. For example, sometimes it is necessary to operate or protect the seed, an internal number used to store state and calculate the next random number. In these special cases, sharing randomly generated objects is inappropriate.

 Concurrency

In the context of a Java EE multi-threaded application, randomly generated instance objects can still be stored in classes or other implementation classes as a static property. Fortunately, java.util.Random is thread-safe, so there is no risk of multiple thread calls destroying the seed.

Another thing worth considering is the multi-threaded java.lang.ThreadLocal instance. The lazy approach is to implement a single instance through Java's own 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 a single instance of java.util.Random. However, the long-awaited Java 7 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 methods: single instance / static access, just like Math.random() flexible. ThreadLocalRandom is also faster than any other method of handling high concurrency.

  Experience

Chris Marasti-Georg pointed out:

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

Make the distribution unbalanced, for example: 0.0 – 0.499999 will be rounded to 0, while 0.5 to 1.499999 will be rounded to 1. So how to use the old syntax to achieve the correct balanced distribution, as follows:

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

Fortunately, if we use java.util.Random or java.util.concurrent.ThreadLocalRandom, we don’t have to worry about the above problems.

 Java Practical Project introduces some dangers of incorrect use of java.util.Random API. This lesson tells us not to use:

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

 but to use:

rnd.nextInt(n)


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn