Home > Java > javaTutorial > body text

How to generate random numbers in java

王林
Release: 2020-05-15 16:54:38
Original
3755 people have browsed it

How to generate random numbers in java

Random numbers can be generated through the random() method.

The random() method is used to return a random number, the range of random numbers is 0.0 =< Math.random < 1.0. It is a default method and does not accept any parameters.

(Recommended video tutorial: java video)

Code sample:

public static void main(String[] args)
    {    
        int max=100,min=1;
        int ran2 = (int) (Math.random()*(max-min)+min); 
        System.out.println(ran2);
    }
Copy after login

Recommended tutorial: java entry program

The above is the detailed content of How to generate random numbers in java. 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