In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the random numbers in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
As we all know, random number is one of the most basic features of any programming language. The basic way to generate a random number is the same: generate a random number between 0 and 1. It seems simple, but sometimes we overlook some interesting features.
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 through the above information, the developer to generate a double-precision floating-point number between 0.0 and 10.0 will write as follows:
Math.random () * 10
When an integer between 0 and 10 is generated, it is written as:
Math.round (Math.random () * 10)
Advanced stage
By reading the source code of Math.random (), or simply taking advantage of IDE's autocomplete feature, developers can easily find that java.lang.Math.random () uses an internal randomly generated object-a very powerful object that can be randomly generated flexibly: Boolean values, all numeric types, and even Gaussian distribution. For example:
New java.util.Random (). NextInt (10)
One drawback of it is that it is an object. Its method must be called through an instance, which means that its constructor must be called first. Expressions like the one above are acceptable if there is plenty of memory, but when there is not enough 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. Guess you may have thought of java.lang.Math, well, we are just improving the initialization of java.lang.Math. Although the amount of this project is low, you should also do some simple unit tests to make sure it doesn't go wrong.
Suppose the program needs to generate a random number to store it, and the problem comes again. For example, sometimes you need to manipulate or protect the seed (seed), and an internal number is used to store the state and calculate the next random number. In these special cases, it is inappropriate to share randomly generated objects.
Concurrence
In the environment of Java EE multithreaded applications, randomly generated instance objects 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 multithreaded java.lang.ThreadLocal. The lazy approach is to implement a single instance through Java's own API, but you can also 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 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, as flexible as Math.random (). ThreadLocalRandom is also faster than any other way to deal with high concurrency.
experience
Chris Marasti-Georg pointed out:
Math.round (Math.random () * 10)
Make the distribution uneven, for example, 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 balanced distribution, as follows:
Math.floor (Math.random () * 11)
Fortunately, we don't have to worry about this if we use java.util.Random or java.util.concurrent.ThreadLocalRandom.
Some of the dangers of incorrect use of java.util.Random API are introduced in the Java actual combat project. This lesson tells us not to use:
Math.abs (rnd.nextInt ()) n
Instead of:
This is the end of rnd.nextInt (n) "what are the random numbers in Java"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.