Which java library provides a means to create a unique random string combination from a given character set?

Which Java library has a tool for creating a unique random string combination from a given character set?

Let's say I have this character set: [a-zA-Z0-9]

And I need to generate a 4-character string from this set, which is less likely to collide.

+4
source share
3 answers

Apache Commons Lang has a RandomStringUtils class using a method that takes a sequence of characters and counts, and does what you ask for. However, this does not guarantee collision avoidance, and with only 4 characters you will fight for it.

+8
source

And I need to generate a 4-character string from this set, which is less likely to collide.

Less likely than? There are 62 ^ 4 = 14.8 million of such lines. Due to the paradox of the day, you get a 50% chance of a collision if you randomly generate 3800 of them. If this is unacceptable, no library will help you, you need to use a longer line or set uniqueness explicitly (for example, by incrementing an integer and formatting it in the base 62).

+3
source

if you feel good with a longer hash, you can probably find some md5 libraries. This is most common for this kind of task. Many websites use it to generate password hashes.

+1
source

Source: https://habr.com/ru/post/1381474/


All Articles