我试图获得一个独特的随机x位置来绘制位图,我唯一的问题是随机数非常相似。那么如何使每个数字之间的这些唯一数字彼此不同,数字应为5 10 15 20 25。
private String generateXPoistion(String poistion, int numberaOfDigits)
{
for(int i=0;i<numberaOfDigits;i++)
{
poistion += randomGenerator.nextInt(9) + 1; // the 1 to avoid zero
}
return poistion;
}
//In the method storeApp, I am using the KeyValue of the hashMap as an x position to draw the bitmap.
public void storeApple(Apple apple)
{
String newId = generateXPoistion("",3);
while(appleMap.get(newId) != null )
{
newId = generateXPoistion("",3);
}
int foo = Integer.parseInt(newId);
apple.setPos(foo);
appleMap.put(newId,apple);
}
答案 0 :(得分:0)
你在哪里
poistion += randomGenerator.nextInt(9) + 1
将其更改为
poistion += 5 * (randomGenerator.nextInt(9) + 1)
1,2,3 ......的结果将缩放为5,10,15 ......