Home  >  Article  >  Java  >  How to generate random numbers in java

How to generate random numbers in java

王林
王林Original
2020-05-15 16:54:383707browse

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);
    }

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!

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