Java随机数,没有重复

时间:2013-06-20 02:59:04

标签: java android

我需要在6种不同的edittext中生成随机数。不幸的是随机数重复。我需要我设定范围内的唯一数字。

        rndNumber = rndNumbers.nextInt(max);
        num1.setText(Integer.toString(rndNumber));


        rndNumber = rndNumbers.nextInt(max);
        num2.setText(Integer.toString(rndNumber));

        rndNumber = rndNumbers.nextInt(max);
        num3.setText(Integer.toString(rndNumber));

        rndNumber = rndNumbers.nextInt(max);
        num4.setText(Integer.toString(rndNumber));

        rndNumber = rndNumbers.nextInt(max);
        num5.setText(Integer.toString(rndNumber));

        rndNumber = rndNumbers.nextInt(max);
        num6.setText(Integer.toString(rndNumber));

5 个答案:

答案 0 :(得分:2)

包装随机数生成器以将结果存储在一个集合中。生成数字时,如果它存在于集合中,请再次执行:

其他人对for循环的评论也是有效的......

伪代码:

class MyRandomNums {
    Set<Integer> usedNums;

    public int getRandom()
    {
        int num = random.nextInt();
        while(usedNums.contains(num)) {
            num = random.nextInt();
        }
        usedNums.add(num);
        return num;
    }

    public int reset()
    {
        usedNums.clear();
    }
}

答案 1 :(得分:0)

所有的for循环都循环一次。原因:

for (int nbr = 1; nbr < 2; nbr++) {
    rndNumber = rndNumbers.nextInt(max);
    num1.setText(Integer.toString(rndNumber));
}

当你可以做的时候:

rndNumber = rndNumbers.nextInt(max);
num1.setText(Integer.toString(rndNumber));

答案 2 :(得分:0)

int min = 0;
int max = 100;
List<Integer> randoms = new ArrayList<Integer>();
for(int i = min; i <= max; i++) randoms.add(i);
Collections.shuffle(randoms);

然后你可以像这样使用它:

int randomNumber = randoms.remove(0);

答案 3 :(得分:0)

这是完成你提到的所有事情的一种方法:

private static boolean arrayContainsInt(int array[], int val, int x) {
    for(int i = 0; i < x; i++) {
        if(array[i] == val) {
            return true;
        }
    }
    return false;
}

public static int[] randomNumbers(int count, int minInclusive, int maxNonInclusive) {
    int randoms[] = new int[count];
    Random rand = new Random();
    int impossibleValue = minInclusive - 1;
    for(int i = 0; i < randoms.length; i++) {
        randoms[i] = impossibleValue;
    }
    for(int x = 0; x < count; x++) {
        int thisTry = impossibleValue;
        while(thisTry == impossibleValue || arrayContainsInt(randoms, thisTry, x)) {
            thisTry = (int)(rand.nextFloat() * (float)(maxNonInclusive - minInclusive)) + minInclusive;
        }
        randoms[x] = thisTry;
    }
    return randoms;
}

如果您对速度感到疑惑,可以参考以下内容:

randomNumbers(1000, 0, 10000) ==&gt; 16毫秒

(1000个随机数,从0-> 10,000,没有重复)

randomNumbers(10000, 0, 10000) ==&gt; 207毫秒

(以0-> 10,000的每个数字以随机顺序生成)

randomNumbers方法提供了要生成的随机整数的数量,最小值(包括)和最大值(不包括),并在给定参数的情况下返回一个随机数组。

注意:如果提供的count参数大于maxNonInclusive - minInclusive的值,那么它将无限循环,因为您无法提供比范围提供的更多的整数。

此外,arrayContainsInt被发送x参数作为要检查的数组中的最大索引之后的值,因为只填充了x个数组的元素,因此检查其余的毫无意义。

答案 4 :(得分:0)

对于字母数字编号,请尝试此

public String getRandomNum(int randomLength)
{   
    return new BigInteger(130, random).toString(32).toUpperCase().substring(0, randomLength);
}

如果你只想要数字随机数,试试这个

public String getRandomNum(int randomLength)
{
    int value = (int)(Math.random() * 9999);
    String format = "%1$0"+randomLength+"d";
    String randomNum = String.format(format, value);
    return randomNum;
}
相关问题