18.4k views
3 votes
Assume that rand is an object of the Random class.

Which of the following statements generates a random number in the [-101..-41] range?
(A) int number = rand.nextInt(61) - 101;
(B) int number = rand.nextInt(61) - 41;
(C) int number = rand.nextInt(-41) - 101;
(D) int number = rand.nextInt(-101) - 41;

User Ghada
by
7.9k points

1 Answer

4 votes

Final answer:

The correct statement that generates a random number in the [-101..-41] range is (A) int number = rand.nextInt(61) - 101

Step-by-step explanation:

The correct statement that generates a random number in the [-101..-41] range is (A) int number = rand.nextInt(61) - 101;

To understand why, let's break down the statement:

  • rand.nextInt(61): This generates a random integer between 0 and 60.
  • rand.nextInt(61) - 101: This subtracts 101 from the random number generated in the previous step, resulting in a number between -101 and -41.

Therefore, option (A) is the correct statement to generate a random number in the specified range.

User Johnjbarton
by
8.4k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.

9.4m questions

12.2m answers

Categories