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.