Math class is part of java.lang package. Math random method returns random number in fraction of double among 0.0 to 1.0. random function return type is double. With random function we can generate random number easily. Generating integer and long random number example is shown to create random number.
Math random example explains how to use Math random function in java.
public class JavaMathRandomExample { public static void main(String[] args) { double E1 = Math.random(); long E2 = Math.round(Math.random()*10); // 1 digit random long E3 = Math.round(Math.random()*100); //2 digit random long E4 = Math.round(Math.random()*1000); //3 digit random System.out.println("Math random value :"+E1); System.out.println("Math random value :"+E2); System.out.println("Math random value :"+E3); System.out.println("Math random value :"+E4); } }
Output
Math random value :0.8393086905318781
Math random value :3
Math random value :82
Math random value :719
Tags: Math



Link to Us