Several random numbers in Java

Source: Internet
Author: User
Tags mathematical functions

Abstract: As we all know, random numbers are one of the most basic features of any programming language. The basic method for generating random numbers is the same: generate a random number between 0 and 1. It seems simple, but sometimes we ignore some interesting features. As we all know, random numbers are one of the most basic features of any programming language. The basic method for generating 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 is to generate a random number in Java. lang. math. random () in all other languages, generating random numbers is like using Math tools, 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 between 0.0 and 1.0. Based on the above information, the developer needs to generate a double-precision floating point number between 0.0 and 10.0, which will be written like this: Math. random () * 10 generates an integer between 0 and 10, which is written as Math. round (Math. random () * 10) advanced by reading Math. random () source code, or simply use the IDE's automatic function, developers can easily find that java. lang. math. random () uses an internal random generation object-a very powerful object can be flexibly randomly generated: Boolean value, All numeric types, or even Gaussian distribution. For example, new java. util. Random (). nextInt (10) 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 the memory is insufficient, 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. I guess you probably think of java. lang. Math. That's good. We just improved the initialization of java. lang. Math. Although the workload is low, you have to do some simple unit tests to ensure that they do not make mistakes. Assuming that the program needs to generate a random number for storage, the problem arises again. For example, you sometimes need to operate or protect the seed. An internal number is used to store the status and calculate the next random number. In these special cases, it is not appropriate to share the randomly generated objects. Concurrent in the Java EE multi-threaded application environment, 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 that multiple thread calls will destroy the seed. Another example worth considering is multi-threaded java. lang. ThreadLocal. The method of laziness is to implement a single instance through Java APIs. 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. as flexible as random. ThreadLocalRandom is faster than any other method that processes high concurrency. Experience Chris Marasti-Georg points out: Math. round (Math. random () * 10) Unbalanced distribution. For example, 0.0-0.499999 is rounded to 0, and 0.5-1.499999 is rounded to 1. Then, how can we use the old syntax to achieve correct balanced distribution? Math. floor (Math. random () * 11) Fortunately, if we use java. util. random or java. util. concurrent. threadLocalRandom does not have to worry about the above issues. The Java practice project introduces some dangers of improper use of java. util. Random APIs. This lesson tells us not to use Math. abs (rnd. nextInt () % n instead of rnd. nextInt (n)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.