0

I'm trying to make a random Integer generator that produces unique numbers.

Does anyone has any ideas how's that possible?

7
  • 3
    I do, but you should show that you have actually tried something... Commented May 13, 2013 at 9:43
  • @MitchWheat doesn't uniqueness means it won't be truly random? Commented May 13, 2013 at 9:44
  • What do you mean by unique, globally unique, or just once you've personally created it you don't want annother one the same Commented May 13, 2013 at 9:45
  • Maybe the poster wants a GUID? Commented May 13, 2013 at 9:45
  • 1
    @Thihara The sequence as a whole will be a random permutation of a finite set. Commented May 13, 2013 at 9:45

4 Answers 4

4

If you have a small-enough range of possible numbers, then this is very simple and useful:

final List<Integer> sack = new ArrayList<>(RANGE);
for (int i = 0; i < RANGE; i++) sack.add(i);
Collections.shuffle(sack);

Now just pull items out of sack in order.

Sign up to request clarification or add additional context in comments.

Comments

1

One simple algorithm would be:

  1. create a HashSet<Integer> set
  2. generate a random number
  3. if not present in set => use that random number and store it in set
  4. if present in set, return to step 2

Note that the numbers won't be truly random because of the uniqueness constraint.

1 Comment

This is only to be recommended it you don't plan to exhaust the set because the performance painfully degrades as the unstruck numbers get scanty.
1

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

1 Comment

@user2375278 lol, fair enough. You see now why I wanted to know exactly what you meant by unique
0

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.

  1. Create a container
  2. Put in it n integer in order.
  3. Shuffle the items in container.
  4. Read elements form it in order.

If you have choose as container a List you can shuffle it using Collections.shuffle()

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.