0

As I understand it, there are two main classes of Java that are able to perform pseudorandom activites: the Random class and the SecureRandom class. Both generate pseudorandom numbers. However, the SecureRandom class generates cryptographically-secure numbers.

Why even use the Random class when you can use the SecureRandom class, which generates more unpredictable numbers? Many programmers (especially video game programmers) use solely the Random class, but both of the classes have almost the same core method (both can generate a pseudorandom integer). Both classes can also be seeded with a long value.

My only explanation is that the Random class operates faster than the SecureRandom class. Is this true? If not, then why do many Java programmers prefer the Random class than the SecureRandom class?

  • 2
    [To start with](http://docs.oracle.com/javase/7/docs/api/java/security/SecureRandom.html): *"[...] Depending on the implementation, the generateSeed and nextBytes methods **may block** as entropy is being gathered [...]"*. Also, `Random` provides a deterministic random sequence and is presumably faster in most implementations. – aioobe Feb 11 '17 at 22:32

1 Answers1

1

There is a number of likely reasons

  • Random is older so there is more examples.
  • Random is random enough for many cases and much faster.
  • Random produces the same result on all platforms, and always the same results for a seed (The default SecureRandom can be different if you don't also specify the strategy)
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Also, in some testing situations, you want to repeat the same series of random numbers. You can do that with `Random` by giving it the same seed. You cannot do that with `SecureRandom`. – rossum Feb 12 '17 at 11:19
  • 1
    @rossum it is possible with some SecureRandom strategies, but can't rely on the default strategy to do this. – Peter Lawrey Feb 12 '17 at 11:51