通过for循环循环时,该循环的长度为1-10。我也经历了一个while循环。我试图找到1-42之间的6个随机数。然后我再做一次(1-10)次。目前,这6个随机数保持不变。我怀疑它与种子有关,但我无法解决。
我尝试同时使用SecureRandom,Random和通过Math.random() 什么都没有。
模型:
public static final int MAX_TIPS = 10;
public static final int MIN_TIPS = 1;
public static final int REQUIRED_NUMBERS = 6;
public static final int RANGE_NUMBER_MIN = 1;
public static final int RANGE_NUMBER_MAX = 42;
public static final int RANGE_LUCKY_NUMBER_MIN = 1;
public static final int RANGE_LUCKY_NUMBER_MAX = 6;
private Random random = new Random();
private int getRandomInt(int min, int max) {
return random.nextInt((max - min) + 1) + min;
}
public ArrayList<LottoTip> createOpponentTips() {
ArrayList<Integer> opponentChosenNumbers = new ArrayList<>();
ArrayList<LottoTip> lottoTips = new ArrayList<>();
//1-10
int amountOfTips = getRandomInt(MIN_TIPS, MAX_TIPS);
for (int i = 0; i < amountOfTips; i++) {
int[] arrayOpponentChosenNumbers = new int[REQUIRED_NUMBERS];
while (opponentChosenNumbers.size() < REQUIRED_NUMBERS) {
//1-42 This here is the problem zone
int randomNumber = getRandomInt(RANGE_NUMBER_MIN, RANGE_NUMBER_MAX);
if (!opponentChosenNumbers.contains(randomNumber)) {
opponentChosenNumbers.add(randomNumber);
}
}
//1-6 ---This here works
int opponentLuckyNumber = getRandomInt(RANGE_LUCKY_NUMBER_MIN, RANGE_LUCKY_NUMBER_MAX);
for (int j = 0; j < opponentChosenNumbers.size(); j++) {
arrayOpponentChosenNumbers[j] = opponentChosenNumbers.get(j);
}
lottoTips.add(new LottoTip(arrayOpponentChosenNumbers, opponentLuckyNumber));
}
return lottoTips;
}
LottoTip:
public class LottoTip {
private int[] numbers;
private int luckyNumber;
public LottoTip(int[] numbers, int luckyNumber) {
this.numbers = numbers;
this.luckyNumber = luckyNumber;
}
public String getNumbers() {
return numbers[0] + ", " + numbers[1] + ", " + numbers[2] + ", " + numbers[3] + ", " + numbers[4] + ", " + numbers[5];
}
public void setNumbers(int[] numbers) {
this.numbers = numbers;
}
public int[] getNumbersArray() {
return numbers;
}
public int getLuckyNumber() {
return luckyNumber;
}
public void setLuckyNumber(int luckyNumber) {
this.luckyNumber = luckyNumber;
}
@Override
public String toString() {
return "\nNumbers: " + getNumbers() + " Lucky Number: " + luckyNumber;
}
}
这是一个System.out.println,因为您可以看到数字保持不变。您能帮我找到一种创建可以一直工作的getRandomInt方法的方法吗?
非常感谢您的帮助。
Kerry York [
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 2,
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 4,
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 5,
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 1,
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 3,
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 6]
Don Dickinson [
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 6,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 5,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 3,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 2,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 4,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 1,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 1,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 4,
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 3]
Clifford Weinstein [
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 6,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 2,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 1,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 4,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 5,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 6,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3,
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3]
Angela Spencer [
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 6,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 3,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 2,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 4,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 5,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 4,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 2,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 2,
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 3]
Leroy McIntosh [
Numbers: 5, 23, 2, 24, 28, 3 Lucky Number: 1]
答案 0 :(得分:4)
您可以在外部for循环的迭代之间重用opponentChosenNumbers
列表。
这样,在第二次迭代中,while循环后卫
while (opponentChosenNumbers.size() < REQUIRED_NUMBERS) {
立即为假,因此您不会选择新的随机数。
将opponentChosenNumbers
的声明移至for循环中(或确保在while循环之前为空,例如,通过调用opponentChosenNumbers.clear()
)。