27

How to generate unique ID that is integer in java that not guess next number?

Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108

11 Answers11

56

How unique does it need to be?

If it's only unique within a process, then you can use an AtomicInteger and call incrementAndGet() each time you need a new value.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
9
int uniqueId = 0;

int getUniqueId()
{
    return uniqueId++;
}

Add synchronized if you want it to be thread safe.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    Might want to add an exception for when it the integers get too high and roll over. Also might want to start uniqueId at Integer.MinInt() (Method is called something like that). – sixtyfootersdude Feb 01 '10 at 23:08
  • 1
    This will work only when the process runs forever without stopping. For restarted apps, it will start from 0 each time. – talonx Feb 11 '10 at 06:41
  • Well, one might think about using a Long instead of Integer in the first place. – Jan Bodnar Dec 11 '16 at 13:52
8
 import java.util.UUID;

 public class IdGenerator {
    public static int generateUniqueId() {      
        UUID idOne = UUID.randomUUID();
        String str=""+idOne;        
        int uid=str.hashCode();
        String filterStr=""+uid;
        str=filterStr.replaceAll("-", "");
        return Integer.parseInt(str);
    }

    // XXX: replace with java.util.UUID

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(generateUniqueId());
            //generateUniqueId();
        }
    }

}

Hope this helps you.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
user1614168
  • 195
  • 2
  • 2
  • 3
    -1. While a UUID will certainly provide a unique value, there's no guarantee that this uniqueness guarantee persists when you take its hashcode. You may as will pick a random number between Integer.MIN_VALUE and Integer.MAX_VALUE and hope for the best. – Simon Nickerson Aug 23 '12 at 09:58
  • 1
    Not so. UUID.randomUUID() uses a cryptographically strong random number generator, so that should be better than using a weaker pseudorandom number generator. – Christopher Barber Feb 17 '13 at 20:49
4

It's easy if you are somewhat constrained.

If you have one thread, you just use uniqueID++; Be sure to store the current uniqueID when you exit.

If you have multiple threads, a common synchronized generateUniqueID method works (Implemented the same as above).

The problem is when you have many CPUs--either in a cluster or some distributed setup like a peer-to-peer game.

In that case, you can generally combine two parts to form a single number. For instance, each process that generates a unique ID can have it's own 2-byte ID number assigned and then combine it with a uniqueID++. Something like:

return (myID << 16) & uniqueID++

It can be tricky distributing the "myID" portion, but there are some ways. You can just grab one out of a centralized database, request a unique ID from a centralized server, ...

If you had a Long instead of an Int, one of the common tricks is to take the device id (UUID) of ETH0, that's guaranteed to be unique to a server--then just add on a serial number.

Bill K
  • 62,186
  • 18
  • 105
  • 157
2

If you really meant integer rather than int:

Integer id = new Integer(42); // will not == any other Integer

If you want something visible outside a JVM to other processes or to the user, persistent, or a host of other considerations, then there are other approaches, but without context you are probably better off using using the built-in uniqueness of object identity within your system.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
1

Just generate ID and check whether it is already present or not in your list of generated IDs.

Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
1

UUID class

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
Jim Barrows
  • 3,634
  • 1
  • 25
  • 36
  • That doesn't return an integer. Format is hexstring, like `20e8f3d6-3f8d-475b-8c19-9619a78bbdc8`. – BalusC Feb 01 '10 at 18:21
  • 1
    @BalusC: This is merely the String output you're talking about. Internally a UUID is a 128-bit value and hence could be interpreted as an integer (albeit BigInteger). – Adamski Feb 01 '10 at 18:30
  • 1
    Still, it doesn't fit in an integer. – BalusC Feb 01 '10 at 18:39
1

Do you need it to be;

  • unique between two JVMs running at the same time.
  • unique even if the JVM is restarted.
  • thread-safe.
  • support null? if not, use int or long.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Bill K. gave a solution for the first case, i.e. what can be done if want the id to be unique between 2 JVMs. What's the right approach to make sure that the id is unique even after JVM restarts? – user1071840 Jul 20 '14 at 15:34
  • Oh, requesting a uniqueId from one of a centralized databases will work after restarts too. – user1071840 Jul 20 '14 at 15:38
0

if only int is required then AtomicInteger can make it possible.

if String is needed then the below code should work by mixing timeStamp and AtomicLong.

AtomicLong idCounter =  new AtomicLong(100);
long timestamp = System.currentTimeMillis();
long nextLong = idCounter.incrementAndGet();
String randomId = String.valueOf(timestamp)+String.valueOf(nextLong); 
0

Imagine you have a class called employee with these attributes:

public class Employee {
    
    private final String name;
    private int id;
    private static int nextID = 1;
    
    public Employee(String name) {
        this.name= name;
        id = nextID++;
    }
}

Easy peasy

-1

Unique at any time:

int uniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85
  • 2
    Referring to [this answer](http://stackoverflow.com/a/2979239/344480), `System.currentTimeMillis()` does not return unique values. – Matthias Oct 20 '14 at 19:30