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.
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
) 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.
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.
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.
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.
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.
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).
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