6 Replies Latest reply: Jan 7, 2005 5:24 AM by 807596 RSS

    Random things

    807596
      hi ! this question might be as basic as it gets but it's something I can't get to work
      let's say I have a Dice class and I need two instances
      each Dice object has a private random generator

      now I think java gives Random instances a seed based on the time, but I really need to create my two dices at the same time
      it would goes like
      Dice d1 = new Dice();
      Dice d2 = new Dice();
      then the constructors create a new Random object
      private Random rnd;
      Dice(){
      rnd = new Random();
      }
      I also call their getRndValue() methods at the same time, and of course, the two numbers are always the same
      how could I prevent this ?
        • 1. Re: Random things
          796440
          Make the random a class variable (a static member variable) that's initialized at declaration, (and get rid of the new Random() in Dice's constructor). Then both dice will share the same pseudo-random number generator.

          Throwing two dice at once is the same as throwing one die and then throwing it again, so there's no reason each one need its own Random.
          • 2. Re: Random things
            807596
            Throwing two dice at once is the same as throwing one
            die and then throwing it again, so there's no reason
            each one need its own Random.
            OP could use a static rng, then.

            The best way to avoid seeding your Randoms with the same seed is to not let the Randoms seed themselves. In 1.5 the problem was fixed, you could use its solution:
            private static long de_samilizer = 0x83276534673;//some number, it doesn't matter
            private Random rnd;
            Dice()
            {
               rnd = new Random(System.currentTimeMillis() + de_samilizer++);
            }
            Please note that there will be significant correlation between your dice. Not that you will notice or care, though...
            • 3. Re: Random things
              807596
              Seed the 2 randoms with the time in millis (what the default constructor does anyway) multiplied by 2 diff numbers...
              • 4. Re: Random things
                796440
                Or just use one Random.
                • 5. Re: Random things
                  807596
                  Or just use one Random.
                  Touche
                  • 6. Re: Random things
                    807596
                    i.e. This works even if the
                    public class Dice {
                        private static final Random rnd = new Random();
                        private final int sides;
                    
                        public Dice() {
                            this(6);
                        }
                    
                        public Dice(int sides) {
                            this.sides = sides;
                        }
                    
                        public int getRandom() {
                            synchronized (rnd) {
                                return rnd.nextInt(sides) + 1;
                            }
                        }
                    }