I want to create a unique number of "Long" type using java. I have seen few examples but they were using timestamp, without using timestamp can i create a unique number of wrapper object "Long" .Please suggest.
please suggest.Thanks.
I want to create a unique number of "Long" type using java. I have seen few examples but they were using timestamp, without using timestamp can i create a unique number of wrapper object "Long" .Please suggest.
please suggest.Thanks.
Generate each digit by calling random.nextInt. For uniqueness, you can keep track of the random numbers you have used so far by keeping them in a set and checking if the set contains the number you generate each time.
public static long generateRandom(int length) {
Random random = new Random();
char[] digits = new char[length];
digits[0] = (char) (random.nextInt(9) + '1');
for (int i = 1; i < length; i++) {
digits[i] = (char) (random.nextInt(10) + '0');
}
return Long.parseLong(new String(digits));
}
Without using timestamp, you have these options:
Many of the answers suggest using Math.random()
to generate the unique id. Now Math.random()
is actually not random at all, and does in itself not add anything unique. The seemingly uniqueness comes from the default seeding in the Math.random()
based on System.currentTimeMillis();
with the following code:
/**
* Construct a random generator with the current time of day in milliseconds
* as the initial state.
*
* @see #setSeed
*/
public Random() {
setSeed(System.currentTimeMillis() + hashCode());
}
So why not just remove the Math.Random()
from the equation and just use System.currentTimeMillis()
in the counter.
The following code implements a unique number generator based solemnly on time. The benefit of this is that you don't need to store any counters etc. The numbers generated will be unique under the following condition: The code only runs in one JVM at any time periode - this is important, as the timestamp is part of the key.
public class UniqueNumber {
private static UniqueNumber instance = null;
private long currentCounter;
private UniqueNumber() {
currentCounter = (System.currentTimeMillis() + 1) << 20;
}
private static synchronized UniqueNumber getInstance() {
if (instance == null) {
instance = new UniqueNumber();
}
return instance;
}
private synchronized long nextNumber() {
currentCounter++;
while (currentCounter > (System.currentTimeMillis() << 20)) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
return currentCounter;
}
static long getUniqueNumber() {
return getInstance().nextNumber();
}
}
The code allows for up to 2^20 numbers to be generated per millisecond (provided you have access to that fast hardware). If this rate is exceeded the code will sleep until next tick of System.currentTimeMillis()
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(UniqueNumber.getUniqueNumber());
}
}
1472534126716256257
1472534126716256258
1472534126716256259
1472534126716256260
1472534126716256261
1472534126716256262
1472534126716256263
1472534126716256264
1472534126716256265
1472534126716256266
Take a look on this Commons Id, it has LongGenerator that generates an incrementing number as a Long object.
This will create simply a random long number -
System.out.println((long)((Math.random())*1000000000000000000L));
You can generate random numbers using java.util.Random
and add them to a java.util.Set
this will ensure that no duplicate is allowed