Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Correct way to use Random.class  (Read 773 times)
0 Members and 1 Guest are viewing this topic.
Offline EatenByAGrue

Junior Member


Medals: 3



« Posted 2013-04-02 23:26:19 »

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:
1  
2  
3  
4  
5  
6  
7  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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  Huh).

It is pitch black. You are likely to be eaten by a grue.
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2013-04-02 23:34:23 »

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

Offline arnaud_couturier
« Reply #2 - Posted 2013-04-03 00:32:00 »

learn stuff properly
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)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2013-04-03 01:02:00 »

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.
Offline StumpyStrust
« Reply #4 - Posted 2013-04-03 04:20:05 »

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.

Online Roquen
« Reply #5 - Posted 2013-04-03 06:09:23 »

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

Junior Member


Medals: 3



« Reply #6 - Posted 2013-04-03 08:47:02 »

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 Cheesy) 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.
Thank you Smiley

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 Cranky

It is pitch black. You are likely to be eaten by a grue.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!