This article mainly introduces the method of generating random numbers in Java, which has a good reference value. Let’s take a look at it with the editor
1. Use System.currentTimeMillis() to get a long number of milliseconds in the current time.
long a = System.currentTimeMillis(); System.out.println(a);
2. Return a double value between 0 and 1 through Math.random().
int b = (int)(Math.random()*99+1); System.out.println(b);
3. Generate a random number through the Random class.
Random random = new Random(); int c = random.nextInt(100)+1; System.out.println(c);
【Related recommendations】
2. Geek Academy Java Video Tutorial
3. Alibaba Java Development Manual
The above is the detailed content of Share three java code tutorials for generating random numbers. For more information, please follow other related articles on the PHP Chinese website!