Random numbers in Java
In Java, we can use Java util. Random class to generate a random number. It has two constructors, random () and random (long seed). Random() uses the current time, system Currenttimemillis() is used as the seed of the generator, while random (long seed) uses the specified seed as the seed of the generator. After the random number generator is generated by the random object, different types of random numbers can be obtained by calling different methods through the object: nextint(), nextlong(), nextfloat(), nextdouble(). If two random objects use the same seed (for example, 100) and call the same function in the same order, their return values are exactly the same. It's useless to talk. Let me give you a chestnut first, as shown in the following code:
But what if I want a range of numbers? For example, I want to randomly generate random numbers between 0 and 99. At this time, we can use the modulus operator%. The purpose of applying the modulus operator% to the random number generated by the random number generator is to make the maximum value of the random number fall within the minus 1 range of the operand value we have formulated. Look at the following code to control the input in the range of 0 ~ 99. (friendly note: if math. ABS () is not added, the output range will be - 99 ~ 99.) Look first without math ABS () is as follows:
In view of the added conditions, the following are: