bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Generate Random Numbers

How To Generate a Random Number

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):

Math.random();

To get more control over the random number, for example, if you only want a random number between 0 and 100, you can use the following formula:

int randomNum = (int)(Math.random() * 101); // 0 to 100

Related Pages

Java Math Reference

Java Type Casting

Previous

Java How To Check Armstrong Number

Next

Java How To Count Words