-6

I want to generate a random id which users will utilize, meaning i want every user to have one and i want it to be easy and completely random (also don't want to repeat itself twice)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
captindfru
  • 233
  • 1
  • 3
  • 13
  • 4
    Have you tried anything? – ItamarG3 May 05 '18 at 15:46
  • @ItamarG3 What do you mean? like to make an array that generates random numbers and letters? because an array generates using a sort of algorithm for generating meaning at one point in time it will repeat itself, and i don't want to happen – captindfru May 05 '18 at 15:48
  • Have you tried any code? Have you made any attempt to solve your problem before asking for help and *guidance* here? – ItamarG3 May 05 '18 at 15:48
  • I tried to look for generating random IDs and/or completely random letters, but an array would repeat itself – captindfru May 05 '18 at 15:50
  • 1
    @captindfru Post the code, showing the attempts to avoid duplication and adapt the question to the part where the duplication-avoidance does not work. That will help others help you. Now it looks like you are asking others to write your code. – M. le Rutte May 05 '18 at 15:58
  • I've used the code from [here](https://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string?rq=1) but as the birthday paradox exists, i don't want it to happen and my random number is comprised of 5 letters – captindfru May 05 '18 at 16:02
  • When you post a question you should tell us what you have already tried. This is not a place where you say what you want to get done, wait a few minutes and simply get the code. – 92AlanC May 05 '18 at 16:03
  • 1
    If you generate big enough random number/string probability is astronomically low. It's industry standard called UUID / GUID (just like AlanC92 answered already). – Risord May 05 '18 at 16:05
  • @user766304 Yes, but i want to the id comprised of only 5 letters only, since this is the id which a user will us. – captindfru May 05 '18 at 16:08

4 Answers4

1

Try this:

long id = System.currentTimeMillis();
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
0

Try UUID.randomUUID().toString();

92AlanC
  • 1,327
  • 2
  • 14
  • 33
0

(You will want to take a look at the Random class in java. It provides random numbers, which is very useful.

To answer your question, I would suggest having a String of allowed characters in the ID, and constructing a new string using a StringBuilder, by selecting random characters from your ALLOWED_CHARACTERS string. Something like

private static final String ALLOWED_CHARACTERS = "ABCD...789";
private static final Random RNG = new Random();
public static String getID() {
    StringBuilder builder = new StringBuilder();
    while (builder.length() < DESIRED_LENGTH) {
        builder.append(ALLOWED_CHARACTERS.charAt((int)(RNG.nextDouble()*ALLOWED_CHARACTERS.length())));
    }
    return builder.toString();
}

Also, while unlikely, you might get some duplicates. If you really want to prevent duplicates, you can use a HashSet of String to see if your ID is already used. By putting your ID's into a HashSet, you can efficiently check if an ID has already been generated.

Dmich
  • 130
  • 6
0

You can use SecureRandom class to generate random numbers. This class is presents in java.security.SecureRandom. Concatenate some letters or word to form an ID.