The example of this article tells the method of Java's designated range. Share it for everyone for your reference. The specifics are as follows:
When writing the Java program, I often use random numbers, but I have always checked it once. This time, let's summarize it. This article is just fine.
There are two classes in Java that can generate random numbers:
java.util.random and java.math.random
Java.util.random in the Java utility library provides methods to produce various types of random numbers. It can produce random numbers such as int, long, float, double, and givingian. This is also the biggest difference between it and the method Random () in Java.lang.math, and the latter only generates the random number of Double type.
The method in Random is very simple. It has only two constructive methods and six ordinary methods.
Construction method:
(1) Public Random ()
(2) Public Random (Long SEED)
Random () uses the current time System.currenttimememillis () as the seeds of the generator, and Random (Long SEED) uses the specified seed as the seeds of the generator.
Java generates a random number requires a base value SEED. In the first method, the base value default, then the system time is used as seed.
Ordinary method:
(1) Public Synonformized Void Setseed (Long SEED)
This method is set the base value seed.
(2) Public int nextint ()
This method produces a integer random number.
(3) Public Long Nextlong ()
This method produces a LONG type random number.
(4) Public Float NextFloat ()
This method produces a Float random number.
(5) Public double nextDouble ()
This method produces a Double -type random number.
(6) Public synchronized double nextgousian () ()
This method is to produce a Double -type GOUSSIAN random number.
If the two RANDOM objects use the same seeds (such as 100) and call the same functions in the same order, they return the value exactly the same. As if the output of the two RANDOM objects in the following code is exactly the same
The random number within the specified range
The random number is controlled within a certain range, using the modulus operating symbol%
Import java.util.*; Class Testrandom {Public Static Void Main (String [] ARGS) {Random Random = New Random (); for (Int I = 0; I <10; I <++) tln (math .abs (random.nextint ())%10);}}}
The random number obtained is positive and negative. Use math.abs to obtain the data range of the obtained data as a non -negative number of non -repeated random numbers within the specified range
Import java.util.*; Class Testrandom {Public Static Void Main (String [] ARGS) {int [] intrt = new int [6]; Intten = 0; // Stall the random number int count = 0; // Record The generated random number int FLAG = 0; // Whether it has generated a logo (count <6) {random rdm = new random (system.currentTimememillis ()); intrd = math.abs (rdm.nextIntINT ()) %32+1; for (int i = 0; i <count; i ++) {if (intrt [i] == intrd) {flag = 1; break;} else {flag = 0;}}}}}}}}}}} 0) {intrt [count] = intrd; count ++;}} for (int T = 0; t <6; t ++) {system.out.println (t+"->"+int [t]);}}}}}}}}}
You can also have nextfloat and so on. All kinds of basic types are available
Math.random can also be written like a random number between 0-10
Copy code code as follows: (int) (math.random ()*10);
Java generates random number of specified range
Numbers between min-max
Random random = new rate (); int Top = random.nextint (maxtop-mintop + 1) + mintop;
Another principle of implementation:
Math.round (math.random ()*(max-min)+min) long test; // cannot be set to int, it must be set as long // to generate random number temp = math.round (math.round (math .random ()*8999+1000);
It is hoped that this article is helpful to everyone's Java program design.