0

Possible Duplicate:
Java random always returns the same number when I set the seed?

In my game there is 4 monsters which are moving to the same random generated coordinates. It means random is not working.

 public void run() {
    while (true) {

                    // paints all Sprites, and graphics
        updateScreen(getGraphics());

        try {
            Thread.sleep(sleep);

        } catch (Exception e) {
        }
    }
}


private void updateScreen(Graphics g) {

     loops through all monsters and moves them a bit
     for (int gi = 0; gi < bottX.length; gi++) {

      bot(gi); // moves a  specified monster or gets new coordinates
     }

}

private void bot(int c) {

// some stuff to move a monster



 // if a monster is in desired place, generate new coordinates

    if(isInPlace()){
       // g]randomly generates new coordinates X and Y
        botX(c);
    botY(c);

    }


 }



public void botX(int c) {

           // monsters walking coordinates are between 30 px from the spawn zone.
    Random r1 = new Random();
    int s = r1.nextInt(3);
    // number 0 - left 1 - right 2 - don`t go in X axis
            // monster spawn coordinate
    int botox = spawnnX[c];
    int einamx;


    if (s == 0) {

        einamx = r1.nextInt(30) + (botox - 30); 
                    // [botox-30; botox)
    } else if (s == 1) {
        einamx = r1.nextInt(29) + (botox + 1); // (botoX+1 botoX+30]
    } else {
        einamx = botox;
    }



            // sets to where the monster should go
    einammX[c] = einamx;
    return;




          }

So in this game is 4 monsters which spawn coordinates are equals and you can only see 1 monter because they are moving the same. BTW if I set spawn coordinates different i can see 4 monsters which are moving the same.

Community
  • 1
  • 1
user1494517
  • 311
  • 1
  • 3
  • 11

4 Answers4

1

I suspect strongly that Random does work! From here

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

and

Two Random objects created within the same millisecond will have the same sequence of random numbers.

So I suspect you're creating multiple Random instances with the same seed, and so you're getting the same sequence of random numbers each time.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

With older versions of Java 1.4.2 and previous, and possibly the version you are using, the Random was seeded with the System.currentTimeMillis() This meant if you creating multiple Random to within the resolution of this call, between 1 and 16 ms, you would get the same seed.

A simple work around is to create one Random and re-use it. This is more efficient and ensures each call will give a random value.

Java 5.0 fixed this by using System.nanoTime() and an AtomicLong counetr to ensure the seed is different each time.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Try to create one Random and reuse that instead of creating a new every time (if you have multiple threads create one per thread).

Peter
  • 37,042
  • 39
  • 142
  • 198
1

It is probably a seed issue. Use the new Random(seed) constructor.

Perhaps you could call it with something like

new Random(System.currentTimeMillis());

although if you call this multiple times within the same millisecond you will have the same issue. I dont think you will need to call multiple times though. Just reuse the object.

RNJ
  • 15,272
  • 18
  • 86
  • 131