I want to know how to get a 4-digit random number in Java. I was trying to do it and everytime I run the program I always get the same number.
Thanks
I want to know how to get a 4-digit random number in Java. I
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Math.random() is a method that generates a random number through a formula. It returns a double however, so casting is required if you want an integer, float, or etc. The if block makes sure that the number is above 1000 and is a 4-digit number.