programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Algorithm to generate random token ids.

Ranch Hand
Posts: 419
• Number of slices to send:
Optional 'thank-you' note:
Hi All,

I want to know what kind of algorithm I can use to generate a random token which is combination of String and Integer.

Ranch Hand
Posts: 1183
• Number of slices to send:
Optional 'thank-you' note:
you could create an array of available characters then iterate for n steps (where n is the number of characters you want the token to consist of) and append a random item from the array to the token.

Ranch Hand
Posts: 182
• Number of slices to send:
Optional 'thank-you' note:
Math.random() ???

Sebastian Janisch
Ranch Hand
Posts: 1183
• Number of slices to send:
Optional 'thank-you' note:
exactly

Ranch Hand
Posts: 53
• Number of slices to send:
Optional 'thank-you' note:
You can use priority Queue also with the combination of Random class.

Ranch Hand
Posts: 78
• Number of slices to send:
Optional 'thank-you' note:

This method returns unique ID which is combination of String and Integer.

Marshal
Posts: 75716
354
• Number of slices to send:
Optional 'thank-you' note:
Are you using truly random numbers (which entail possible duplications) or do you want random sampling from a diminishing population, in which case your values will be unique? For random sampling from a diminishing population, put values into a List (probably via a Set to avoid duplicates) and remove the value selected; the List will gradually become smaller, so your random numbers must be chosen from a diminishing range.
Alternative: shuffle a List and choose the first n values.
Remember the Random class and Math.random() are not truly random, but do present possible duplications. If you use Math.random() long enough, you are guaranteed to receive duplicate values.

Sheriff
Posts: 22650
126
• Number of slices to send:
Optional 'thank-you' note:
That's true for each (pseudo) random number generator. Given a possible N values, there is always a chance of 1 out of N that the next value will be exactly the same.

pawan chopra
Ranch Hand
Posts: 419
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:Are you using truly random numbers (which entail possible duplications) or do you want random sampling from a diminishing population, in which case your values will be unique?

we do not want any duplications. Actually we have a user name based on that we want to generate a token id.

Campbell Ritchie
Marshal
Posts: 75716
354
• Number of slices to send:
Optional 'thank-you' note:
Do you know the user names will be free from duplications?

pawan chopra
Ranch Hand
Posts: 419
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:Do you know the user names will be free from duplications?

Yes. we are sure about that. I am doing like this for now. username+username.hashcode(). It will be unique.

Rob Spoor
Sheriff
Posts: 22650
126
• Number of slices to send:
Optional 'thank-you' note: