Correct way to use Random.class

Hey fellas,

I’m using java.util.Random for lots of different things in my game (rolling dice, choosing different enemies, names, etc.). I have read that it is usually a good idea to use one source of random numbers (incase you want to save the seed for example as far as I understood).

In my “older” game I had a Dice.class that in which I used:


public static Random rnd = new Random();

// and called 
{
    Dice.rnd ... 
}
// everywhere else

In my latest try (in which I really want to take my time and learn stuff properly instead of wanting to finish a game as fast as possible) I now switched to a singleton pattern:


public class RandomInstantiation
{
	final private static Random instance = new Random();
	
	private RandomInstantiation() {};
	
	public static Random getInstance() {
		return instance;
	}
}

I’d love to know if this is a proper way to do this, as I’ve also read that usually singleton patterns aren’t encouraged… (whatever that means ???).

That’s fine or you can use Math.random() which returns a double value between 0 (inclusive) and 1 (exclusive).

You’re doing the worst thing for a programmer: applying patterns without knowing why, or just to feel smarter.
Patterns may solve problems (sometimes), but on the other hand they make code more complex and abstract (for sure!), therefore more buggy.

You should concentrate on the KISS principle at all times: Keep It Simple Stupid.

If you don’t need a random seed NOW, then don’t bother with them. Keep your code as simple and straightforward as you can, because it’ll always be too complex as some point, and always sooner than we’d like.

But if you know FOR SURE you’ll need seeds, then yes, you may need a singleton, though I hate them.
I prefer to keep an object around (your random nb generator) instead.

Singletons are simply global state hidden behind a trendy name.
And global state will eat you alive without you noticing ! (<= that’s why singletons are evil)

If you don’t need to reproduce a PRNG sequence from a seed, there’s nothing really wrong with a singleton random instance used across scopes.

You’ll get somewhat better performance by using an instance of Random yourself such as the singleton, and not the static methods on Random. If you’re working in multiple threads, using ThreadLocalRandom in 1.7 and later is also a bonus.

I use a singleton type of class. Basically, I have a static class that has a Random object with a bunch of convenience methods for random numbers. For example, to get a random number including fractions from 0-10 I would Rnd.randomPosf(number). If I want and int Rnd.randomPosi(number). If I want it to include negative it would be Rnd.randdomNegf(). etc etc etc etc

Don’t worry too much about what is “right” because if it works it is right. Singletons are just sorta a bad practice. Same goes with static things but in reality you do what works and is fast to write.

If you’re going to use a JDK supplied RNG and 1.7+ is OK, then ThreadLocalRandom is a better choice even for single threaded apps (IMHO).

I used the Singleton here because I wanted only one instance of random instead of creating multiply objects in every class (or method) I needed it. But I really should follow the KISS-principle (great band btw :D) and I know that I am far from having to worry about “performance”. It just felt “bad” to create a bunch of new random isntances everywhere.

Would you mind explaining how it looks? I mean do you use a private static Random field in that class?
Many thanks you for your reply.

Thank you :slight_smile:

Yes, I have read about that here in the forum yesterday but I’m on MacOS 10.6.8 so I only have JDK1.6 available atm :cranky: