[英]How to generate 20 unique random numbers with order using Java?
am new to programming and I just started doing a program where I want to generate and sort 20 unique random numbers, how ever I was only able to generate numbers which were not unique nor sorted using this script 我是编程的新手,我刚开始做一个程序,我想要生成和排序20个独特的随机数,我怎么只能生成不唯一的数字,也不能使用这个脚本排序
import java.util.Random;
class FTW {
public static void main (String[]args){
Random I = new Random();
int number;
for(int counter=1; counter<=20;counter++){
number = I.nextInt(20);
System.out.println(number + " ");
}
}
}
Can any one help me edit this or can give me a better one that does the job and explain it to me if possible :) 任何人都可以帮我编辑这个或者可以给我一个更好的工作,并在可能的情况下向我解释:)
Using a HashSet will preserve the uniqueness 使用HashSet将保留唯一性
This will provide a random set of 20 numbers from 1-50 这将提供从1-50开始的随机20个数字集
public static void main(final String[] args){
final Random random = new Random();
final Set<Integer> intSet = new HashSet<>();
while (intSet.size() < 20) {
intSet.add(random.nextInt(50) + 1);
}
final int[] numbers = new int[intSet.size()];
final Iterator<Integer> iter = intSet.iterator();
for (int i = 0; iter.hasNext(); ++i) {
numbers[i] = iter.next();
}
System.out.println(Arrays.toString(numbers));
}
One way is to add the numbers to an ArrayList and check if it contains the next random number in a while-loop. 一种方法是将数字添加到ArrayList并检查它是否包含while循环中的下一个随机数。 (These are 20 unique random numbers from 0-100)
(这些是从0到100的20个唯一随机数)
public class FTW {
public static void main (String[]args){
Random I = new Random();
List<Integer> list = new ArrayList<Integer>();
int number;
for(int counter=1; counter<=20;counter++){
number = I.nextInt(100);
while(list.contains(number)) {
number = I.nextInt(100);
}
list.add(number);
}
Collections.sort(list); //Sorts the list
System.out.println(list);
}
}
You may follow these steps: 您可以按照以下步骤操作:
1. maintain an ArrayList
to store the generated unique random numbers ( uniqueSortedRandoms
). 1.维护一个
ArrayList
来存储生成的唯一随机数( uniqueSortedRandoms
)。
2. While inserting a new number newNumber
into this ArrayList
, check if the array uniqueSortedRandoms
contains the newNumber
and whether the newNumber
is greater than or equals to the previous one. 2.在将新数字
newNumber
插入此ArrayList
,检查数组uniqueSortedRandoms
是否包含newNumber
以及newNumber
是否大于或等于前一个。 I as assuming the array is sorted ascending order. 我假设数组按升序排序。 See the following code -
请参阅以下代码 -
import java.util.Random;
class FTW {
public static void main (String[]args){
Random I = new Random();
int newNumber;
List<Integer> uniqueSortedRandoms = new ArrayList<Integer>();
for(int counter=1; counter<=20;){
int previousNumber = -1 // initially set to -1
// because nextInt() can be
//range from 0 (inclusive) to 20 exclusive
newNumber = I.nextInt(20);
if(newNumber>previousNumber && !unqueSortedRandoms.contains(newNumber)){
uniqueSortedRandoms.add(newNumber);
previousNumber = newNumber;
counter++;
}
System.out.println(number + " ");
}
}
}
Now the ArrayList
- uniqueSortedRandoms
contains all the unique random numbers you need in an ascending order. 现在,
ArrayList
- uniqueSortedRandoms
按升序包含您需要的所有唯一随机数。
Note: 注意:
1. If you use random.nextInt(20)
then it will generate a random number from 0 inclusive to 20 exclusive. 1.如果你使用
random.nextInt(20)
那么它将生成一个从0到20的独立的随机数。 And you need 20 random numbers in a sorted order. 并且您需要按排序顺序排列20个随机数。 So the array List actually contains 20 numbers from 0 to 19. In this case you can just generate the
arrayList
with numbers from 0 to 19.Or if you need 20 random numbers in sorted order (not in the range 0 to 19) like [3, 4, 9, 10, ......] then you may use a very large int
as a parameter of nextInt(int n)
- 所以数组列表实际上包含从0到19 20个号码在这种情况下你可以生成
arrayList
编号从0到19.Or如果你需要在排序顺序20张随机数(不在范围0〜19),例如[ nextInt(int n)
,......]那么你可以使用一个非常大的int
作为nextInt(int n)
的参数 -
newNumber = I.nextInt(100);
Now the each newNumber
will be in the range - 0<=newNumber<100
. 现在每个
newNumber
将在0<=newNumber<100
的范围内。 So your array will contains 20 unique random number in a ascending order. 因此,您的数组将按升序包含20个唯一的随机数。
2. The counter
is incremented inside the if-block
(when the newly generated random number is inserted int the ArrayList
) so that the loop continues until we get the 20th random number. 2.
counter
在if-block
内递增(当新生成的随机数插入ArrayList
),循环继续,直到我们得到第20个随机数。
There is way how to generate unique, sorted numbers without sorting. 有办法如何生成唯一的,排序的数字,而无需排序。 You can create array of boolean and which number you choose, that index is true.
您可以创建布尔数组和您选择的数字,该索引为true。
public static void main(String[] args) {
int maxNumber = 150;
int totalCount = 20;
Random random = new Random();
boolean[] generatedNumbers = new boolean[maxNumber];
int generatedCount = 0;
while (generatedCount < totalCount){
int newNumber = random.nextInt(maxNumber);
if (generatedNumbers[newNumber] == false){
generatedNumbers[newNumber] = true;
generatedCount++;
}
}
int[] sortedUniqueArray = new int[totalCount];
int selectedNumbers = 0;
for (int i = 0; i < generatedNumbers.length; i++) {
if (generatedNumbers[i] == true){
sortedUniqueArray[selectedNumbers] = i;
selectedNumbers++;
}
}
System.out.println(Arrays.toString(sortedUniqueArray));
}
Output for this sample is : 此示例的输出是:
[6, 19, 33, 47, 51, 53, 71, 75, 82, 86, 89, 92, 105, 108, 121, 125, 126, 137, 140, 147]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.