[英]How to generate a list of distinct random numbers including a specific number in JAVA?
好的,所以場景是,我想生成一個包含4個distinct
隨機數的列表,這些列表將代表測驗應用程序的4個隨機選擇。 四個隨機選擇之一將是正確的答案,因此我們將已經知道正確選擇的索引。 此正確的索引或數字必須包含在隨機數列表中。
例如:考慮我們有一個長度為100的array
,其中包含代表一個問題的100個選擇的string
值,正確選擇的index
為45
。 現在,我們需要為該問題提供4個隨機選擇,包括索引45,這樣索引列表將類似於{2,91,45,17}。 此外,列表中不應包含重復的數字。
知道如何在Java中實現嗎?
對於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方法,因為據我所知, Collections.shuffle
是Java 6中引入的。
我首先建議使用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()));
不工作!
這節課可以幫助你
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時剩下的差距。如果選擇了一些數字,我們選擇一個新的數字,然后我們需要在該數字下方添加空白數量,即已經選擇的較小或相等數字的數量來應對所有空白。
請注意,沒有什么私人的事情,如果別人的正確答案也被否決,我將留下我留下的評論。 我不是反對我的答案被否決,而是反對總體上否決正確答案。
我根據您的需要編寫了完整的程序。 但是,請看一下我在做什么。 只需要一點上下文,這就是我創建的:
// 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
如果您解釋將在何處使用它,以及對已編碼內容的簡短演示,將很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.