Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


How can you ensure generating a different sequence of random numbers each time in Java?

  1. Using System.currentTimeMillis() as a seed

  2. Not providing any seed

  3. Hardcoding a seed value

  4. Calling reset() method on Random object

The correct answer is: Using System.currentTimeMillis() as a seed

By using System.currentTimeMillis() as a seed, the random numbers will be generated based on the current system time. This ensures that each time the code is run, a different seed value will be used, resulting in a different sequence of random numbers. Option B is incorrect because not providing a seed will result in the same sequence of random numbers being generated each time the code is run. Option C is incorrect because hardcoding a seed value will also result in the same sequence of random numbers being generated each time. Option D is incorrect because calling reset() on a random object will only reset the seed to its initial value, and therefore, result in the same sequence of random numbers being generated each time.