简体   繁体   中英

How to generate apache-like unique ids?

I want to mimic the mod_unique_id in my java program, so I would need to generate random IDs like:

XpCR9wraCg0AABLBjI8AAAAh

Is there any built-in module to attain this?

Perhaps a good starting point is to change the radix of a random UUID...

String uniqueID = new BigInteger(UUID.randomUUID().toString().replace("-",""), 16)
                      .toString(36) //change radix to 36
                      .toUpperCase()

36 as radix makes use of all letters of the alphabet in addition to digits 0-9. Case does not make a difference here, so I upper-cased the result (it's a preference)

Example values:

242H0VS3CSMQR3SS9FHLFKR07
BADXWCCZQ44DB4K8QCN67HF6J
CT3Z8R6JGYOLRKTQSFINWQQ8A

Do you mean something like this

UUID.randomUUID().toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM