Java wont seem to give me random numbers. Here is my code:
Random rand = new Random();
boolean spaceFilled = false;
while (!spaceFilled) {
switch (rand.nextInt(400) % 4) {
case 0: if (topOpen) {
spaceFilled = true;
mazeArray[currentWidth - 1][currentHeight + 1] = 2;
mazeArray[currentWidth][currentHeight + 1] = 1;
mazeArray[currentWidth + 1][currentHeight + 1] = 2;
currentPathsArray[currentPath][1] = currentHeight + 1;
System.out.println("top");
}
break;
case 1: if (bottomOpen) {
spaceFilled = true;
mazeArray[currentWidth - 1][currentHeight - 1] = 2;
mazeArray[currentWidth][currentHeight - 1] = 1;
mazeArray[currentWidth + 1][currentHeight - 1] = 2;
currentPathsArray[currentPath][1] = currentHeight - 1;
System.out.println("bot");
}
break;
case 2: if (leftOpen) {
spaceFilled = true;
mazeArray[currentWidth - 1][currentHeight + 1] = 2;
mazeArray[currentWidth - 1][currentHeight] = 1;
mazeArray[currentWidth - 1][currentHeight - 1] = 2;
currentPathsArray[currentPath][0] = currentWidth - 1;
System.out.println("left");
}
break;
case 3: if (rightOpen) {
spaceFilled = true;
mazeArray[currentWidth + 1][currentHeight + 1] = 2;
mazeArray[currentWidth + 1][currentHeight] = 1;
mazeArray[currentWidth + 1][currentHeight - 1] = 2;
currentPathsArray[currentPath][0] = currentWidth + 1;
System.out.println("right");
}
break;
}
}
However, while the case that is called changes each time I run the program, it calls the same case repeatedly throughout the same running of the program, meaning that it is getting the same random number over and over... Why is that? I also tried Math.random, but with the same results. Thank you for any help.
Aucun commentaire:
Enregistrer un commentaire