Home  >  Article  >  Java  >  Share three java code tutorials for generating random numbers

Share three java code tutorials for generating random numbers

Y2J
Y2JOriginal
2017-05-11 09:29:571757browse

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】

1. Java Free Video Tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn