[course07]04 Random

[course07]04 Random


// range 0.0 <= x < 6.0
double x1 = 6 * Math.random();
System.out.println(x1);

// range 2.0 <= x < 3
double x2 = Math.random() + 2;
System.out.println(x2);

// range 4.0 <= x < 6.0
double x3 = 2 * Math.random() + 4;
System.out.println(x3);

// 0 to 99
int num1 = (int) (Math.random() * 100);
System.out.println(num1);

// 1 to 100
int num2 = (int) (Math.random() * 100) + 1;
System.out.println(num2);

// 5 to 24
int num3 = (int) (Math.random() * 20) + 5;
System.out.println(num3);

Last updated

Was this helpful?