This is a small enhancement to yours but should be resilient.

Essentially, we use the current time in milliseconds unless it hasn't ticked since the last id, in which case we just return last + 1.

private static final long LIMIT = 10000000000L;
private static long last = 0;

public static long getID() {
  // 10 digits.
  long id = System.currentTimeMillis() % LIMIT;
  if ( id <= last ) {
    id = (last + 1) % LIMIT;
  }
  return last = id;
}

As it is it should manage up to 1000 per second with a comparatively short cycle rate. To extend the cycle rate (but shorten the resolution) you could use (System.currentTimeMillis() / 10) % 10000000000L or (System.currentTimeMillis() / 100) % 10000000000L.

Answer from OldCurmudgeon on Stack Overflow
๐ŸŒ
Javapractices
javapractices.com โ€บ topic โ€บ TopicAction.do
Java Practices->Generating unique IDs
import java.rmi.server.UID; public class UniqueId { /** * Build and display some UID objects. */ public static void main (String... arguments) { for (int idx=0; idx<10; ++idx){ UID userId = new UID(); System.out.println("User Id: " + userId); } } }
Discussions

How to generate Unique ID using Java Programming?
Find answers to How to generate Unique ID using Java Programming? from the expert community at Experts Exchange More on experts-exchange.com
๐ŸŒ experts-exchange.com
September 2, 2004
uniqueidentifier - How do I create a unique ID in Java? - Stack Overflow
I'm looking for the best way to create a unique ID as a String in Java. Any guidance appreciated, thanks. I should mention I'm using Java 5. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Make unique id of just numbers? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
๐ŸŒ stackoverflow.com
Generating 10 digits unique random number in java - Stack Overflow
I am trying with below code to generate 10 digits unique random number. As per my req i have to create around 5000 unique numbers(ids). This is not working as expected. It also generates -ve numbers. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ generating-10-digits-unique-random-number-in-java
How to Generate 10-Digit Unique Random Numbers in Java: Fixing Negative Values & Missing Digits for 5000 Unique IDs โ€” javathinking.com
Weโ€™ll break down the problem, explore solutions, and provide a step-by-step implementation with code examples. ... 10-digit: The number must be between 1,000,000,000 (inclusive) and 9,999,999,999 (inclusive).
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 21116439 โ€บ How-to-generate-Unique-ID-using-Java-Programming.html
Solved: How to generate Unique ID using Java Programming? | Experts Exchange
September 2, 2004 - Hello, I refer to the codes send by cjjclifford. import java.util.Random; class MeowTest{ private Random rand; public MeowTest() { rand = new Random( System.currentTimeMillis() ); } public String generate() { String[] digit = new String[6]; String result = ""; for (int i = 0; i < 6; i++) { digit[i] = Integer.toString(rand.next ยท Int( 9 )); result += digit[i]; } return result; } public static void main( String[] args ) { MeowTest gen = new MeowTest(); for( int i = 0; i != 10; i++ ) { System.out.println( gen.generate() ); } } } I hope that helps....
๐ŸŒ
GitHub
github.com โ€บ aventrix โ€บ jnanoid
GitHub - aventrix/jnanoid: A unique string ID generator for Java. ยท GitHub
// Use a faster, but non-secure, random generator Random random = new Random(); // Use a custom alphabet containing only a, b, and c char[] alphabet = {'a','b','c'}; // Make IDs 10 characters long int size = 10; String id = NanoIdUtils.randomNanoId(random, alphabet, 10); // "babbcaabcb" Code copyright 2017 The JNanoID Authors, Aventrix LLC, and Andrey Sitnik. Code released under the MIT License. Based on the original NanoId for JavaScript by Andrey Sitnik.
Starred by 516 users
Forked by 58 users
Languages ย  Java
Find elsewhere
๐ŸŒ
Pega
support.pega.com โ€บ question โ€บ how-generate-10-digit-unique-id-sequential-order
How to generate 10 digit unique ID in sequential order | Support Center
March 2, 2017 - When generating the unique WO id, it internally calls stored procedures sppc_data_uniqueid which inturn calls sppc_data_uniqueid_withouttran. This checks the value of pyLastReserved of pc_data_uniqueid and it increments value by 1every time. ... You need to write utility for achieving this.write java code for generating 10 digit unique random number.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-generate-a-unique-ID-in-Java
How to generate a unique ID in Java - Quora
Using UUID class: UUID stands for "Universally Unique Identifier" and is a 128-bit value that is guaranteed to be unique. You can generate a UUID in Java using the java.util.UUID class as follows:
๐ŸŒ
Medium
medium.com โ€บ @mounikakurapati17 โ€บ how-to-generate-unique-number-in-java-ab3acfa82973
How To Generate Unique Number in Java | by Mounika | Medium
December 5, 2022 - Java provides different ways to generate random numbers, using some built-in methods and classes, but most of them do generate unique positive long numbers, like the java.util.Random class, the Math.random method, the ThreadLocalRandom class, and many others. But in most of these cases, uniqueness is not followed, which means a random number generated by those methods is not unique. With a UUID, it is usually possible to generate a unique ID that is a string.
๐ŸŒ
Medium
medium.com โ€บ @ganesh.shah โ€บ uniqueid-generator-in-distributed-systems-implementation-in-java-thread-safe-dbc7ff1fbd36
UniqueID Generator in distributed systems | Implementation in Java ( Thread-Safe ) | by GANESH SHAH | Medium
July 27, 2011 - import java.time.Instant; public class UniqueIdGenertorBitManipulation { private static final long EPOCH_OFFSET = Instant.parse("2024-01-01T00:00:00Z").toEpochMilli(); private static final int DATACENTER_ID_BITS = 5; private static final int NODE_ID_BITS = 5; private static final int SEQUENCE_BITS = 12; private static final long MAX_DATACENTER_ID = (1L << DATACENTER_ID_BITS) - 1; private static final long MAX_NODE_ID = (1L << NODE_ID_BITS) - 1; private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1; private long lastTimestamp = -1L; private long sequence = 0L; private final long da
๐ŸŒ
Medium
medium.com โ€บ @shivangi.pandey285 โ€บ generate-unique-positive-long-number-f6d8734023a3
Generate Unique Positive Long Number | by Shivangi Pandey | Medium
April 13, 2023 - To produce a positive unique (with extremely low chances of repetition) long value in Java, the well-known UUID library, which is also widely used for generating unique strings, can be used. With UUID, a unique long number of the desired length can be obtained as below. Approach 1:UUID.randomUUID().toString() produces a unique string of length 36 that is made up of characters (alphabets and โ€œ-โ€) and digits.
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 28935451 โ€บ Generate-UUID-with-14-digit.html
Solved: Generate UUID with 14 digit | Experts Exchange
March 25, 2016 - You can generate a UUID in Java like this: UUID id = UUID.randomUUID(); You can generate as many as you want and they should all be unique - that's the idea of a UUID. By default a Java UUID is 128-bits.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ guide to uuid in java
Guide to UUID in Java | Baeldung
May 15, 2025 - Then weโ€™ll look at the different types of UUIDs and how we can generate them in Java. Learn how to validate a UUID string by using regular expressions or the static method of the UUID class. ... A practical comparison of various UUID generation methods in Java.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 329042 โ€บ java โ€บ Creating-Unique-Id
Creating Unique Id (Java in General forum at Coderanch)
To make unique keys across a cluster ... go to the database once for every 1000 keys. The db sequence number is 10 digits and we format the compound as 00000-00000-000 for human consumption....
๐ŸŒ
UUID Generator
uuidgenerator.net
Online UUID Generator Tool
Quickly and easily generate individual or bulk sets of universally unique identifiers (UUIDs).
๐ŸŒ
GitHub
gist.github.com โ€บ icella โ€บ d4011b6808fc549c538c0310528d9e94
Java โ€“ Ways to Generate Unique Ids in Java
Java โ€“ Ways to Generate Unique Ids in Java ยท Raw ยท CurrentTimeId.java ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.