I'm trying to make a random Integer generator that produces unique numbers.
Does anyone has any ideas how's that possible?
I'm trying to make a random Integer generator that produces unique numbers.
Does anyone has any ideas how's that possible?
One simple algorithm would be:
HashSet<Integer> setset => use that random number and store it in setset, return to step 2Note that the numbers won't be truly random because of the uniqueness constraint.
If what you're looking for is globally unique identifier you might want to consider java's UUID class that produces an ID that will only be created once, ever, across the whole world
import java.util.UUID;
public class Test {
public static void main(String[] args) {
System.out.println(UUID.randomUUID());
}
}
It is, however, alphanumeric not an integer
There is not such thing like unique integer random.
You are limited in the best case to number 2 to power of 64 witch is quite big number.
The problem with unique values is for what do you need them. Do they have to be unique for single application run or do they must stay unique for whole application life ?
So you will have bunch of way to create a random number generator. The UUID is a option but the assyliasaproach is fine too also the Fisher-Yates shuffle is quite good example.
To this collection you can add something like.
If you have choose as container a List you can shuffle it using Collections.shuffle()