Home > Java > javaTutorial > body text

Several interesting uses of Java random numbers

伊谢尔伦
Release: 2016-11-26 09:16:22
Original
1599 people have browsed it

 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()
Copy after login

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
Copy after login

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

Math.round(Math.random() * 10)
Copy after login
Copy after login

 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)
Copy after login

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)
Copy after login

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)
Copy after login
Copy after login

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)
Copy after login

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
Copy after login

 but to use:

rnd.nextInt(n)
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!