如何在Java中生成一个没有重复的随机数

时间:2017-06-29 00:08:37

标签: java

我使用它在方法中生成一个随机数并返回它:

int randomValue = ThreadLocalRandom.current().nextInt(0, filteredArrayList.size());

如何确保连续两个随机数?如果我得到3,然后是5,然后是3,我不在乎。只有我得到3然后是3。

5 个答案:

答案 0 :(得分:1)

    int temp = -1; // This will be your value to be compared to random value
    for(int i = 0; i < 10; i++) { // assuming your filteredArraylist is size 10
        int randomValue = ThreadLocalRandom.current().nextInt(0, 10);
        if(randomValue == temp) {
           i--; // it will repeat the generation of random value if temp and randomValue is same
        } else {
           temp = randomValue; // you will put here the ne random value to be compared to next value
           System.out.println(randomValue);
    }

答案 1 :(得分:1)

尝试以下示例

import java.util.Random;


public class RandomTest {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++) {
            System.out.println("####### " + RandomUtil.getRandomInt(10));
        }
    }
}

class RandomUtil {
    private static int lastInt = -1;
    private static Random random = new Random();

    public synchronized static int getRandomInt(int upperBound) {
        return getRandomInt(0, upperBound);
    }

    public synchronized static int getRandomInt(int lowerBound, int upperBound) {
        int newInt = -1;

        while( (newInt = lowerBound + random.nextInt(upperBound - lowerBound)) == lastInt) {
            //Keep looping
        }

        lastInt = newInt;

        return newInt;
    }
}

答案 2 :(得分:0)

正如Scary Wombat所说,你需要将之前的值与循环中的新随机值进行比较(我假设你正在使用它,因为我们只看到一行。

像这样......

int prevRandomNumber, currRandomNumber;
while (notFinished) {
    currRandomNumber = getRandom(); // your random number generator
    if (prevRandomNumber == currRandomNumber) { // if there would be two in a row
        continue;                               //   try again
    } else {                                    // otherwise, add to array
        addNumberToArray(currRandomNumber);     
        prevRandomNumber = currRandomNumber;
    }
}

答案 3 :(得分:0)

记住最后生成的值,如果等于则拒绝。这是一个例子:

int lastRandomValue = -1;
boolean stop = false;
int attempts = 0;
final int maxAttempts = 100_000;

while (!stop) {
    int currentRandomValue = ThreadLocalRandom.current().nextInt(0, filteredArrayList.size());

    if (currentRandomValue != lastRandomValue) {
        // Use the value
        ...
        // Reset the counter
        attempts = 0;
        ...
        // Stop the generation process if generated enough values
        if (...) {
            stop = true;
        }
    } else {
        // Increment a counter
        attempts++;
    }

    if (attempts >= maxAttempts) {
        stop = true;
    }
}

答案 4 :(得分:0)

<强>编辑: 我做这样的事情:(第一次要求最后一个随机值/不可达的数字,例如-1)

private int notPreviousRandom(int previousRandomValue) {
    int randomValue;
    do {
        randomValue = ThreadLocalRandom.current().nextInt(0, filteredArrayList.size());
    } while (randomValue == previousRandomValue);

    return randomValue;
}

或者,您可以将previousRandomValue定义为属性:

// class
private int previousRandomValue = -1;
// ...

private int notPreviousRandom() {
    int randomValue;
    do {
        randomValue = ThreadLocalRandom.current().nextInt(0, filteredArrayList.size());
    } while (randomValue == previousRandomValue);
    previousRandomValue = randomValue; // for the next time you're using the
                                        // method
    return randomValue;
}