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 Generate Random Numbers
public class Random{
public static void main(String[] args) {
int random = (int )(Math.random() * 50 + 1);
System.out.println(random);
}
}
Possible Output Generate Random Numbers
1 / 5 / 50 / 45 / 15 / 25
Thank you for visiting and hope this helps you. Feel free to leave a comment below for any questions. I got some other articles related to php and mysql here in my blog. Watch out for more upcoming tutorials related to blogging and programming. Good day! ^_^
GENERATE RANDOM NUMBER IN JAVA: Display Random Numbers Using Java Language
No comments:
Post a Comment