Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

589
Views
How to generate random ID numbers that can't be reused in java?

For my current java project, I am trying to generate random ID's for registered users. So far I have been using min +(int) (Math.random()*((max-min)+1)) as my formula to generate the random number. The problem that I am facing is that sometimes the numbers repeat themselves and my application wouldn't work with them.

    int min = 1001;
    int max = 1050;
    
    for (int i=1; i<=1; i++)
    {
    int a = min +(int) (Math.random()*((max-min)+1));
    
    

     }
     
     
  

I have tried using and incorporating

    Integer[] arr = new Integer[100];
    for (int i = 1; i < arr.length; i++) {
    arr[i] = i;
    }
    Collections.shuffle(Arrays.asList(arr));

but numbers generated would constantly come out as "null" and it would repeat the loop a few hundred times and flood my txt file.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In general, random generators Random or Math.random() are not the correct ways to generate a unique id. As you mentioned, it could be repeated (and it will definitely be).

I would recommend two ways of generating ID.

The first one is to use AtomicInteger. This is good when your ID should be unique but not random.

private static final AtomicInteger ID = new AtomicInteger(0);

public static String generateUniqueId() {
    return String.valueOf(ID.incrementAndGet());
}

The second one, which is preferable to me, is to use UUID. This is good when your ID should be as unique as random.

public static String generateUniqueId() {
    return String.valueOf(UUID.randomUUID());
}

Another one, I can mention is to use System.nanoTime().

public static String generateUniqueId() {
    return String.valueOf(System.nanoTime());
}

Long ago I had some investigation and find out that this is pretty stable for normal payload. But in general, it could retrieve the same value if you build such a system, that should generate ID so often.

over 4 years ago · Santiago Trujillo Report

0

Instead of generating numbers I would recommend to generate UUID. The chance of a is collision is close to impossible.

UUID id = UUID.randomUUID();

Otherwise if you want to stick with numbers I would recommend you to implement yourself some Sequence service within your application.

import java.util.concurrent.atomic.AtomicLong;

public class SequenceService {

    private final AtomicLong ids;

    public SequenceService() {

        long initialValue = getInitialValue();
        this.ids = new AtomicLong(initialValue);
    }

    public long generateNextId() {
        return ids.incrementAndGet();
    }

    private long getInitialValue() {
        // this methods reads the last known leased id (e.g. from the file system)
    }
}

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!