Thursday, January 28, 2016

GENERATE RANDOM NUMBER IN JAVA: Display Random Numbers Using Java Language

I already posted the article about generating random numbers using PHP. This time, I converted it into Java language.

The following code will display random number between 1 to 50. Random function will return numbers from 0.0 to 0.999 so multiplying it with 50 and adding 1 to it will result to 1.0 and 50.95. But since you declare it as integer (int) then it will display 1 to 50.

Java Code

public class Random{

public static void main(String[] args) {

int random = (int )(Math.random() * 50 + 1);
System.out.println(random);

}
}
Possible Output
1 / 5 / 50 / 45 / 15 / 25

GENERATE RANDOM NUMBER IN JAVA: Display Random Numbers Using Java Language

GENERATE RANDOM NUMBER IN JAVA: Display Random Numbers Using Java Language Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment