36 ms·
Not too much trouble, but horribly wrong. You are generating a new Random instance with possibly expensive seed generation for every random number, and, even wo
by fhars 17y ago
Not too much trouble, but horribly wrong. You are generating a new Random instance with possibly expensive seed generation for every random number, and, even worse, you are completely at the mercy of the seed generation used by your JVM.
Sun's is relatively good:
public Random() { this(++seedUniquifier + System.nanoTime()); }
But GNU classpath contains:
public Random()
{
this(System.currentTimeMillis());
}
Which will give you the same predictible "random" number for every call within a millisecond.
- lucifer 17y agoThe OP stated: "but sometimes it just isn't worth the trouble to instantiate a Random object". My reply was a counter point to that statement, and not a discourse on using Random.
- deleted 17y ago[deleted]
- RiderOfGiraffes 17y agoThe point is that while it's not too much trouble to do as you did, it is often too much trouble to do it properly. Giving the OP some credit for being a reasonable programmer, your reply is less useful than it might at first appear. Sure, it isn't too much trouble to instantiate a Random object, but to do it properly can be a right pain, and sometimes just isn't worth it.