当用户在java中输入数组的大小时,如何生成从0到100的随机数而不产生重复数?

42fyovps  于 2021-08-25  发布在  Java
关注(0)|答案(5)|浏览(345)

编写一个程序来生成0到100范围内任意数量的随机整数。您的程序应该将大小作为参数,并将数字作为数组返回。
这是我得到的问题,下面是我尝试过的代码,但是我得到了重复的代码。如何生成没有任何重复的随机数?

import java.util.Arrays;
    import java.util.Random;
    import java.util.Scanner;

    public class Main {

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);

            System.out.println("size:\t");
            int n = sc.nextInt();

            int[] arr = new int[n];

            Random rand = new Random(); //instance of a random class.
            int upperbound = 101; //generate random values from 0-100

            for(int i=0;i<n;i++) {
                arr[i] = rand.nextInt(upperbound);
            }
            System.out.println("Random Numbers: "+ Arrays.toString(arr));

        }
    }
ztyzrc3y

ztyzrc3y1#

您可以创建一个包含从0到100的所有值的列表,然后将其洗牌并提取该列表的前n个元素,如下所示:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("size:\t");
        int n = sc.nextInt();

        int bound = 100;

        List<Integer> items = IntStream.range(0, bound + 1)
                .boxed()
                .collect(Collectors.toList());

        Collections.shuffle(items);

        int[] arr = new int[Math.min(n, bound + 1)];
        for(int i = 0; i < arr.length; i++){
            arr[i] = items.get(i);
        }

        System.out.println("Random Numbers: "+ Arrays.toString(arr));
    }
jgzswidk

jgzswidk2#

您可以创建一个1…100的列表(我们称之为“l”)和一个大小为100的数组(我们称之为“a”)。
然后,创建一个for循环并生成一个介于0到l之间的数字。size()-1。
第一次迭代生成(我们称之为index0)将是l中的索引,该索引将作为数组中的第一个对象。因此,调用l.remove(index0),并在[0]中使用检索到的对象。
第二次迭代生成,介于0到l.size()-1之间,表示0到98。这将是index1。调用l.remove(index1),检索到的对象将用于[1]中的第二个索引。
直到我空了为止。

fcwjkofz

fcwjkofz3#

你可以使用 HashSet 并不断向其中添加所有新条目,同时生成新的 random 整数,检查它是否已生成(如果数字在 unique 设置)。
如果号码已经生成,则继续生成新号码,直到找到唯一号码。

Set<Integer> unique = new HashSet<Integer>();
        for (int i=0; i<100; i++)
        {
            int number = rand.nextInt(upperbound);
            while (unique.contains(number)) // `number` is a duplicate
                number = rand.nextInt(upperbound);
            arr[i] = number;
            unique.add(number); // adding to the set
        }
busg9geu

busg9geu4#

这段代码也可以工作

public void solution(int size){

    int upperbound = 101;
    int arr[]=new int[upperbound];

    /**
     * initial a number arr store 0-100
     */
    for (int i = 0; i <upperbound ; i++) {
        arr[i]=i;
    }

    SecureRandom secureRandom=new SecureRandom();
    for (int i = 0; i <size ; i++) {

        /**
         * if generate a index, print number and swap now index number and tail
         */
        int index=secureRandom.nextInt(upperbound-i);
        int tmp=arr[index];
        int last=arr[arr.length-1-i];

        System.out.println(tmp);
        arr[index]=last;
    }

}
kyxcudwk

kyxcudwk5#

您可以使用arraylist,而不是使用数组,并且只有在arraylist以前未使用过的情况下,才可以将该值添加到arraylist中。

ArrayList<Integer> numbers = new ArrayList<Integer>();
    Random random = new Random();
if (!numbers.contains(randomNumber)) {
    numbers.add(randomNumber);
}

在此之后,您可以直接打印arraylist或将其转换为数组,然后将其打印出来。

Integer[] arr = new Integer[number.size()];
    // ArrayList to Array Conversion
    for (int i = 0; i < number.size(); i++) {
        arr[i] = number.get(i);

相关问题