如何在Java中创建从1到10的10个不重复的唯一随机数字?

huangapple go评论74阅读模式
英文:

How to create 10 unique random numbers from 1 to 10 without repeats in Java?

问题

以下是翻译好的部分:

我需要创建一个程序,在Java中生成从0(包括)到10(不包括)之间的10个唯一数字,但是目前我的程序会重复一些数字。我应该如何最好地实现这个?

目前的代码如下:
(请原谅我的代码,我对Java还不太熟悉)

public static int[] UniqueRand (int N)
{
    // 创建一个随机数字列表
    int [] randNums = new int[N];
    Random numRandom = new Random();

    for (int i = 0; i < N; i++){
        randNums[i] = numRandom.nextInt(N);
    }
    return randNums;
}

主函数:
(通常情况下,(n)可以是任何范围,不仅限于10)

public static void main(String [] args)
{
    int n = 10;
    Random newRandom = new Random();
    int [] randNums = UniqueRand(n);
    int [] replace = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    System.out.println();
    for (int i = 0; i < n; i++){
        for (int j = i + 1; j < n; j++){

            if (randNums[i] == randNums[j]){

                for (int k = 0; k < 10; k++){
                    if (randNums[i] != replace[k]) randNums[i] = replace[k];

                }
            }
       }
    }
}

输出将会是:
[9, 1, 5, 9, 0, 2, 4, 6, 1, 9]

英文:

I am required to create a program that creates 10 unique numbers from 0(incl) to 10(excl) in java but so far mine repeats some numbers. what is the best way i can implement this?

Code so far.....
(forgive my code, i'm still a bit new to java)

public static int[] UniqueRand (int N)
{
    //Create a list of random numbers
    int [] randNums = new int[N];
    Random numRandom = new Random();

    for (int i = 0; i &lt; N; i++){
        randNums[i] = numRandom.nextInt(N);
    }
    return randNums;
}

Main Function:
(generally (n) could be any range not just 10)

public static void main(String [] args)
{
    int n = 10;
    Random newRandom = new Random();
    int [] randNums = UniqueRand(n);
    int [] replace = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    System.out.println();
    for (int i = 0; i &lt; n; i++){
        for (int j = i + 1; j &lt; n; j++){

            if (randNums[i] == randNums[j]){

                for (int k = 0; k &lt; 10; k++){
                    if (randNums[i] != replace[k]) randNums[i] = replace[k];

                }
            }
       }
    }
}

Output would be:
[9, 1, 5, 9, 0, 2, 4, 6, 1, 9]

答案1

得分: 1

有两种方法可以解决这种问题,各有优势。您可以生成任何数字并查看是否被使用,在开始时效果很好,但在结束时会变得很差。您可以构建一个候选数字的列表,并在使用后将它们删除,生成一个选择索引,索引的大小为剩余列表的大小,直到大小为1为止。后一种方法可能在一个数组中,但是删除中间成员意味着需要移动其余部分的列表,而链表意味着遍历列表以找到第n个成员。

对于大型列表,可以从方法1开始,然后在使用了大约80%左右时切换到方法2!

生成从0到N的整数是一个简单的数学问题。确保您得到真正随机的选择!作为起点,随机使用浮点数/双精度数是不错的。

英文:

There are 2 ways to do this sort of problem, each with advantages. You can generate any number and see if it is used, great at first and horrible at the end You can build a list of all candidates and remove them as they are used, generating a selector index of the size of the remaining list until the size is 1. The latter might be in an array but removing intermediate members means moving the rest of the list, but a linked list means traversing the list to find the nth member.

for big lists, start with method 1 and switch to method 2 at some point, like 80% used or so!

Generating integers 0 to N is a simple math problem. Make sure you are getting truly random choices! A float/double random is nice as a starting point.

huangapple
  • 本文由 发表于 2020年9月13日 06:07:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63865342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定