这是我用来生成一组随机数字的代码:
...
public boolean placeTreasure() {
randomGen = new Random();
int[] treasureLoc = {0, 0};
while (treasureLoc[0] < 2 || treasureLoc[1] < 2) {
treasureLoc[0] = randomGen.nextInt(rows - 2);
treasureLoc[1] = randomGen.nextInt(columns - 2);
System.out.println("" + treasureLoc[0] + ", " + treasureLoc[1]);
}
maze[treasureLoc[0]][treasureLoc[1]] = '*';
return true;
}
...
有趣的是,它在早期版本的Android上运行得很好。据我所知,4.1以上的任何内容都不能正常运行。它经常给我0, 0
对。这让我相信4.1+不支持随机类,或者我的实现还有其他奇怪的东西。这种方法在早期版本上运行得很好,所以我不确定发生了什么。
如果有人对此替代实施有建议(我需要在2
和rows
或columns
之间生成随机整数。)
答案 0 :(得分:1)
如果有人对此的替代实现有建议(我需要生成2和行或列之间的随机整数)。
是的,非常简单:
int randomRow = randomGen.nextInt(rows - 2) + 2;
int randomCol = randomGen.nextInt(columns - 2) + 2;