0
public static int[] generatePicks(int size, int lowValue, int highValue, long seed) {
    Random rand = new Random(seed);
    int[] arr = new int[size];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = rand.nextInt((highValue - lowValue) + 1) + lowValue;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] == arr[j]) {
                arr[i] = rand.nextInt((highValue - lowValue) + 1) + lowValue;
            }
        }
    }    
    Arrays.sort(arr);
    return arr;   
}

How can i ensure that the newly created random int doesn't match any pre-existing? I don't want to use any methods not pertaining to one-dimensional arrays.

3 Answers 3

0

Try contains:

    public static int[] generatePicks(int size, int lowValue, int highValue, long seed) {
    Random rand = new Random(seed);
    int[] arr = new int[size];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = getRandom(arr, rand, lowValue, highValue);
    }
    Arrays.sort(arr);
    return arr;
}

private static int getRandom(int[] arr, Random rand, int lowValue, int highValue) {
    int nextInt = rand.nextInt((highValue - lowValue) + 1) + lowValue;
    if (Arrays.asList(arr).contains(nextInt)) {
        return getRandom(arr, rand, lowValue, highValue);
    }
    return nextInt;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you correctly you only want to work with array's and no other data type so this is an array only solution although the code could probably be made more efficient if other types and methods was allowed.

This code also handles the situation where the size parameter is larger than the difference between highValue and lowValue to avoid an infinite loop. It handles the situation by changing the size used but maybe a better option would be to throw an exception.

public static int[] generatePicks(int size, int lowValue, int highValue, long seed) {
    Random rand = new Random(seed);

    int usedSize = size > highValue - lowValue ? highValue - lowValue : size;
    int[] arr = new int[usedSize];
    for (int i = 0; i < usedSize; i++) {
        arr[i] = rand.nextInt((highValue - lowValue) + 1) + lowValue;

        for (int j = 0; j < i; j++) {
            if (arr[i] == arr[j]) {
                i--; 
                break;
            }
        } 
    } 
    Arrays.sort(arr);
    return arr;   
}

The inner loop used for checking for duplicates was taken from this answer

Comments

0

Here are three approachs:

1)

Your own approach only with a modified inner loop that goes through all previous random numbers from 0 to i-1 and checks for duplicates. If a duplicate is found just force a generation of a new random number by decrementing the index of the outer loop --i

public static int[] generatePicks3(int size, int lowValue, int highValue, long seed) {
    Random rand = new Random(seed);
    int[] arr = new int[size];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = rand.nextInt((highValue - lowValue) + 1) + lowValue;
        for (int j = 0; j < i; j++) {
            if (arr[j] == arr[i]) {
                --i;
            }
        }
    }    
    Arrays.sort(arr);
    return arr;   
}

2)

Using IntStream(lowValue, highValue+1) and Collections.shuffle(List<?>, rand)

Generate all numbers between lowValue and highValue collect them to list, shuffle the list and get the first n elements, where n = size

public static int[] generatePicks2(int size, int lowValue, int highValue, long seed) {
    Random rand = new Random(seed);
    List<Integer> list = IntStream.range(lowValue, highValue+1).boxed().collect(Collectors.toList());
    Collections.shuffle(list, rand);
    return list.stream().limit(size).mapToInt(Integer::intValue).sorted().toArray();
}

3)

The simplest and shortest way using Random.ints(): generate an infinite stream of random numbers between lowValue and highValue make them unique by applying distinct() and limit the stream to the desiered size.

public static int[] generatePicks(int size, int lowValue, int highValue, long seed) {
    Random rand = new Random(seed);
    return rand.ints(lowValue, highValue).distinct().limit(size).sorted().toArray();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.