Custom Search

Wednesday 23 October 2013

Create a java program to generate random number between 0 to 150.


public class Test {{
public static void main(String args[]){
double random = Math..random(); // Math class is java defined
// class having random method which return random number
random = random * 50;
// as random method is created between 0..0 to 1..0 by multiplying with
// 50 we generated random number between 0..0 to 50..0
int randomInt = (int) random;
// we are explicitly converting double number into int so our random
//number will be between 0 to 50 now.
System.out.println(randomInt);
// printing the random number on the screen.
}
}

No comments:

Post a Comment

Laptops