Home > Java > javaTutorial > body text

Share three java code tutorials for generating random numbers

Y2J
Release: 2017-05-11 09:29:57
Original
1741 people have browsed it

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);
Copy after login

2. Return a double value between 0 and 1 through Math.random().

  int b = (int)(Math.random()*99+1);
  System.out.println(b);
Copy after login

3. Generate a random number through the Random class.

Random random = new Random();
  int c = random.nextInt(100)+1;
  System.out.println(c);
Copy after login

【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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!