Generate 10-character random alphanumeric code

Asked

Viewed 1,352 times

3

I have today an application that is presenting data divergence because of the return time of a webservice.

To get around this we created a code to prevent this particular information from ever repeating, until then everything right is working.

I need to generate a 10-digit identification code, which is not repeated on other tablets that use my application. Because it can compromise the data in the system.

How can I generate a 10-digit random alphanumeric code that does not repeat itself, using Java?

  • 3

    Why don’t you use the UUID of the device?

  • every hour I submit a piece of information, in case a service order I need a different code.

  • Even if they are 10 random digits, there is a chance that the device A and B will generate the same. The idea of @Wakim would solve your problem, because this UUID is unique, I think.

  • 2

    Since each request needs to be unique, it could salt the UUID with a timestamp and generate an MD5. the MD5 can be optional believe.

  • Yes I thought about it, but the service only accepts up to 10 characters in this location.

  • Now thinking here, if I generate MD5 and give break the string in up to 10 characters would work.

  • 1

    I think making substring of MD5 is not a good solution, because prefixes can repeat, even MD5 has collisions. I saw that there is the ANDROID_ID (http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID), which is a 64-bit number, which is unique to each user/device.

  • @Wakim thank you, if you want to put as an answer for me to mark as correct. Ai you complement to take the timestamp the uuid and generate the md5 and take only the top 10 positions of the returned string.

  • 1

    @Hiagosouza, do not recommend using substring on MD5. The MD5 already has a certain probability of collision, using substring will increase a lot (collision occurs when two values generate the same hash). The way is to look for an encryption algorithm that can generate exactly 10 digits, in order not to increase the collisions.

Show 4 more comments

1 answer

8


You can use the UUID and leave 2 digits for you to increment each operation, so you never repeat. Leaves a length available, which you believe will not burst.

Another possibility is to use a fixed number of each device and the seconds of the day that you are using in alphanumeric, for example 00:02 in the morning of the 120sec 003c in case 4 digits.

  • 2

    I think it’s a good answer, not a comment. Since UUID has 8 characters, there are 2 pros Ids left, that is, 65536 transactions, without any risk of repetition. I think it is an excellent solution.

  • 1

    All that remains is to make adjustments to the grammar to be a reasonable answer. You can [Dit] the question to stay in good condition.

  • I’ll have lunch later and do the tests..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.