java is a programming language used by programmers and developers. Many beginners have fallen into a simple question: how to set random numbers when developing using java? How to set random numbers in java? There are often places where random numbers need to be used. Don't worry, let's take a look at the detailed tutorial on setting random numbers in Java.
1. In j2se, we can use the Math.random() method to generate a random number. The generated random number is a double between 0-1. We can multiply it by a certain number. , for example, multiplied by 100, it is random within 100, which is not available in j2me.
2. The java.util package provides a Random class. We can create a new Random object to generate random numbers. It can generate random integers, random floats, random doubles, and random longs. This It is also a method of obtaining random numbers that we often use in j2me programs.
3. There is a currentTimeMillis() method in our System class. This method returns a number of milliseconds from 0:00:00 on January 1, 1970 to the present. The return type is long. We can use it as a random number, we can use it to modulo some numbers, and then limit it to a range
In fact, the third method above is also used in the default construction method of Random Method to generate random numbers
There are following instructions for the Random class in method two:
The java.util.Random class can be constructed in two ways: with seeds and without seeds
Without seed:
This method will return a random number, and the result will be different every time it is run
public class RandomTest {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
}
With seed:
This kind method, no matter how many times the program is run, the return result is the same
public static void main(String[] args) {
java.util.Random r=new java.util.Random (10);
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
}
The difference between the two methods is
(1) First, please open the Java Doc, we will see the description of the Random class:
This Instances of this class are used to generate a stream of pseudo-random numbers. This class uses a 48-bit seed, which can be modified using the linear congruence formula (see The Art of Computer Programming, Volume 2, Volume 2 by Donald Knuth, page 3.2 .1 section).
If you create two Random instances with the same seed, and make the same sequence of method calls to each instance, they will generate and return the same sequence of numbers. In order to ensure the realization of this feature, we specify a specific algorithm for the Random class. For full portability of Java code, Java implementations must make class Random use all the algorithms shown here. But subclasses of the Random class are allowed to use other algorithms, as long as they conform to the general contract of all methods.
The Java Doc has explained the Random class very clearly, and our tests have also verified this.
(2) If the seed number is not provided, the seed number of the Random instance will be the number of milliseconds of the current time. The number of milliseconds of the current time can be obtained through System.currentTimeMillis(). Opening the source code of the JDK, we can see this very clearly.
/**
* Creates a new random number generator. Its seed is initialized to
* a value based on the current time:
* Random() { this(System.currentTimeMillis()); }java.lang.System#currentTimeMillis()
*/
public Random() { this(System.currentTimeMillis()); }
Also:
random Description of the nextInt(), nextInt(int n) method of the object:
int nextInt()
Returns the next pseudo-random number, which is uniformly distributed in the sequence of this random number generator int value.
int nextInt(int n)
Returns a pseudo-random number taken from this random number generator's sequence between 0 (inclusive) and the specified value (exclusive) uniformly distributed int values.
The above is the detailed content of How to set random numbers in java. For more information, please follow other related articles on the PHP Chinese website!