Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random String

 
Anjali S Sharma
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me how can I create a Random String. Every time this method is called it should return a random string. I have a limit of 32 characters.

I tried this



But value of token1 and token2 come as same.
Please help
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using an old version of Java? In Java1.4 and earlier the Random objects took the time of day as their "seed," so creating two Random objects ran the risk of both returning the same "random" values. Even in later versions that might happen, but the risk is less. Suggest you try using only one Random object.
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using Radix as 32?


static String toString(long i, int radix)



Radix means root in Latin. It refers to the base of the numbering system. Because we humans have ten fingers we use a base 10 numbering system (aka decimal, radix 10),
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But value of token1 and token2 come as same.
Please help[/QB]



I am not sure if you missed the following part of API for java.util.Random:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a List (ArrayList), fill it with all the characters you want to randomize, then shuffle (a method from Collections) and retrieve the first 32 characters.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just shuffeling a List is not a good idea, at least not if you have less than 32 instances of every character you can use. This is because with only one instance of every character (or less than 32 instances), the Strings would not be truely random, since results with repetitions of the same characters are impossible, and you will get only permutations of your alphabeth. Using 32 instances of every character, in turn, will cost a lot of memory, in relative terms. So better create you random String char by char, best using a StringBuffer.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Jaspreet Aswal (2nd post): Stick to a single Random object.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guido Sautter:
Just shuffeling a List is not a good idea, at least not if you have less than 32 instances of every character you can use. This is because with only one instance of every character (or less than 32 instances), the Strings would not be truely random, since results with repetitions of the same characters are impossible, and you will get only permutations of your alphabeth. Using 32 instances of every character, in turn, will cost a lot of memory, in relative terms. So better create you random String char by char, best using a StringBuffer.


Good point!
 
Calum Ludwig
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small portion of code that creates an 'n' length string of numbers, lowercase letters, and uppercase letters.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic