A detailed _java of random numbers in Java

Source: Internet
Author: User

In Java we can use the Java.util.Random class to produce a random number occurrence. It has two constructors, namely random () and random (long Seed). Random () is the seed of the generator with the current time, System.currenttimemillis (), and Random (long Seed) is the seed of the generator with the specified seed.

When the random number generator is the random object, the different methods can be called by object invocation: Nextint (), Nextlong (), Nextfloat (), nextdouble (), etc. to obtain different types of random numbers. If 2 random objects use the same seed (for example, all 100) and call the same function in the same order, their return values are exactly the same.
I'll give you a chestnut, the following code:

But what do I want to do with a certain range of numbers? For example I want to randomly generate random numbers between 0-99, this time we can use modulo operator%.
The modulus operator% is applied to the random number produced by the random number generator, and the aim is to let the maximum of the random number fall within 1 of the value of the operand we set. Look at the code below to control the input to the 0~99 range. (Friendship hint: if do not add math.abs (), Output range will be -99~99. )
First look at the situation without Math.Abs (), as follows

In view of the added situation, as follows:

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.