93.3k views
9 votes
Write a print statement that displays a random integer between 5 and 5000. Assume the random library is imported.

User JonnyBoy
by
4.7k points

1 Answer

7 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It is a very simple three line statement (assuming that the random library was imported) that chooses a random integer value between 5 and 5000 and prints it to the screen using the println statement.

Random rand = new Random();

int randomNum = rand.nextInt((5000 - 5) + 1) + 5;

System.out.println(randomNum);

The random number generator is initialized and given a value between 5 and 5000. Since random number Generator will generate a number between 0 and the given value then subtracting 5 from the initial generated number makes sure that it is not more than 5000 and then adding 1 and 5 after wards makes sure that it is more than 5 always.

User Prophet
by
6.1k points