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: