好的,所以场景是,我想生成一个包含4个distinct
个随机数的列表,它代表测验应用程序的4个随机选择。 4个随机选择中的一个将是正确答案,因此我们已经知道正确选择的索引。这个正确的索引或数字必须包含在随机数列表中。
例如: 请注意,我们的array
长度为100,包含代表100个问题选项的string
个值,以及正确选择index
为45
。现在我们想要4个随机选择这个问题,包括索引45,这样索引列表就像{2,91,45,17}。该列表也不应包含重复的数字。
知道如何在Java中实现这个目标吗?
答案 0 :(得分:4)
对于Java 6及更新版本:
final int maxNumber = 100;
final int numbersToGenerate = 4;
final int correctAnswer = 45;
Set<Integer> possibleAnswers = new HashSet<>();
Random random = new Random();
// add correct answer
possibleAnswers.add(correctAnswer);
// add as much random answers as needed, the usage of a set prevents duplicates
while(possibleAnswers.size() < numbersToGenerate) {
possibleAnswers.add(random.nextInt(maxNumber));
}
// convert set to list and shuffle it
List<Integer> answers = new ArrayList<Integer>(possibleAnswers);
Collections.shuffle(answers, new Random(System.nanoTime()));
对于6以下的Java版本,您必须编写自己的shuffle方法,因为据我所知,Java 6中引入了Collections.shuffle
。
我首先建议使用Java 8的随机api,但在我的想法中发现了一个错误。如果生成的随机数数组包含正确答案,则无效。为了您的理解:
不工作!!!
final int minNumber = 1;
final int maxNumber = 100;
final int numbersToGenerate = 3;
final int[] ints = new Random().ints(minNumber, maxNumber)
.distinct().limit(numbersToGenerate).toArray();
List<Integer> possibleAnswers = asList(ints);
possibleAnswers.add(correctAnswerIndex);
Collections.shuffle(possibleAnswers, new Random(System.nanoTime()));
不工作!!!
答案 1 :(得分:1)
这门课可以帮到你
public class RandomQuiz {
//The number of possible answers
private int size;
//The number of possible indexes
private int n;
//The correct index
private int correct;
//Constructor
public RandomQuiz(int size, int n, int correct) {
this.size = size;
this.n = n;
this.correct = correct;
}
//Returns size number of shuffled random indexes
public int[] getRandomIndexes() {
//The result set
int[] result = new int[size];
//We start with the correct index in the first place, so random values will be entered starting from the second place
int index = 1;
//First thing's first
result[0] = correct;
Random random;
while (index < size) {
//We always decrease the number of seeds
random = new Random(n - index);
//Getting a random value
int randomized = random.nextInt();
//Ensuring the numbers are not duplicate
for (int i = 0; i < index; i++) if (randomized >= result[i]) randomized++;
result[index++] = randomized;
}
//Randomize where correct will be at the end:
random = new Random(size);
int newIndex = random.getNextInt();
//If the new index of correct is bigger than 0
//than swap it with the item located on newIndex
if (newIndex > 0) {
result[0] = result[newIndex];
result[newIndex] = correct;
}
return result;
}
}
编辑:
在与安东的私人聊天中,他告诉我有些部分不清楚,即:
randomized
种子数量减少,因为我们可以使用任何一次最大数量。如果种子是100,那么在选择第一个项目之后,它变为99,依此类推。回答第二个问题:如果选择了45,然后选择了至少45的数字,那么我们需要在该数字上加1,以应对我们选择45时留下的差距。如果选择了一些数字,我们选择一个新的数字,然后我们需要在该数字后面加上它下面的间隙数,即已经选择的较小或相等数字的数量,以应对所有间隙。
请注意,我个人没有采取任何措施,如果其他人的正确答案也被拒绝,我会留下我留在这里的评论。我并不反对我的回答被低估,而是反对低估投票的正确答案。
答案 2 :(得分:0)
我根据您的需求编写了完整的程序。但请看看我在做什么。只需一点上下文,这就是我创建的内容:
// initialize a random object once.
Random random = new Random();
// the question
String question = "With what letter does the name start of the new president of the USA?";
// here are some basic answers
String[] answers = new String[] {
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k"
};
// you already know the correct index of the array above, in this case it's d
int index = 3;
// this array will contain four answers, including correct one!
String[] four = new String[4];
// get answer index, we will place correct value in that index
int answerIndex = random.nextInt(four.length);
// now lets pick 4 answers!
for (int i = 0; i < four.length; i++) {
// we are at the answer index!
if (i == answerIndex) {
four[i] = answers[index];
continue;
}
int randomIndex = random.nextInt(answers.length);
for (int j = 0; j < four.length; j++) {
// we got duplicate here!
if (answers[randomIndex].equals(four[j])) {
randomIndex = random.nextInt(answers.length);
// redo this current iteration
j = j - 1;
}
}
four[i] = answers[randomIndex];
}
输出:
e, c, d, h
g, d, d, h
d, g, e, f
d, f, b, i
g, d, a, b
c, d, g, b
h, d, e, k
e, f, d, c
k, d, e, h
i, d, e, d
如果你解释一下你使用它的位置,以及你已编码的简短演示,将会有所帮助。