Java record-38-Random Number

Source: Internet
Author: User

Java record-38-Random Number
Java random number 1. in Java, we can use Math. the random number generated by the random () method is a double between 0 and 1. We can multiply it by a certain number, for example, by 100, he is random within 100. II. in java. the util package provides a Random class. We can create a new Random object to generate a Random number. It can generate Random integers, Random float, Random double, Random long, this is a method we often use in java programs to obtain random numbers. III. there is a currentTimeMillis () method in our System class. This method returns a millisecond value from 00:00:00, January 1, January 1, 1970. The return type is long. We can use it as a random number, we can use him to modulo some numbers to limit him to a certain range. Details: java. lang. math. random; call this Math. the Random () function can return a positive double value. The value is greater than or equal to 0.0 and less than 1.0. That is, the value range is [0.0, 1.0, the returned value is a pseudo-random number selected, which is evenly distributed in this range. Int num = (int) (Math. random () * 3) The num value in the code above will be evenly and randomly distributed! When using Math. Random (), you must note that this function returns the value of the double type. Therefore, you must perform type conversion when assigning values to other types of variables. Java. util. random; in the Java API help document, I summarized the descriptions of the functions of this Random () function: 1. java. util. random Algorithms implemented in the Random class are pseudo-Random, that is, Random Algorithms with rules. The so-called rule is to randomly generate numbers within the given range (seed; 2. for Random objects with the same number of seeds, the Random numbers generated by the same number of times are identical. 3. Random numbers generated by methods in the Random class are evenly distributed, that is to say, the chance of generating numbers within the interval is equal. The following is Java. util. random () method abstract 1. protected int next (int bits): generates the next pseudo-random number. 2. boolean nextBoolean (): returns the next pseudo-random number, which is a boolean value derived from the even distribution of the random number generator sequence. 3. void nextBytes (byte [] bytes): generate a random byte and place it in the byte array provided by the user. 4. double nextDouble (): returns the next pseudo-random number, which is a double value uniformly distributed between 0.0 and 1.0, derived from the sequence of the random number generator. 5. float nextFloat (): returns the next pseudo-random number, which is a float value evenly distributed between 0.0 and 1.0, derived from the sequence of the random number generator. 6. double nextGaussian (): returns the next pseudo-random number, which is a double value in Gaussian ("normal") distribution derived from the sequence of the random number generator. Its average value is 0.0 and the standard deviation is 1.0. 7. int nextInt (): returns the next pseudo-random number, which is the int value evenly distributed in the sequence of the random number generator. 8. int nextInt (int n): returns a pseudo-random number, which is an int value that is evenly distributed between (inclusive and specified (excluded) values from the sequence of the random number generator. 9. long nextLong (): returns the next pseudo-random number, which is a long value from the even distribution of the random number generator sequence. 10. void setSeed (long seed): use a single long seed to set the seed of the random number generator. Conclusion: Let's briefly compare the features of these two random functions: 1. java. math. random () actually calls java internally. util. random (), which has a fatal weakness, is related to system time, that is, two random with short time intervals, such as: double a = Math. random (); double B = Math. random (); you may get two identical double values. 2. java. util. Random () can implement the same functions as java. Math. Random () during calling. It also has many calling methods and is relatively flexible. So in general, using java. util. Random () is relatively flexible. Next article: http://www.bkjia.com/kf/201510/447449.html

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.