This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Random String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random String" Watch "Random String" New topic
Author

Random String

Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Sep 05, 2007
Posts: 52
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),


Still Learing..
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52

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

Joined: Jul 13, 2007
Posts: 171
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.


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
Agree with Jaspreet Aswal (2nd post): Stick to a single Random object.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
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

Joined: Oct 01, 2007
Posts: 14
Here is a small portion of code that creates an 'n' length string of numbers, lowercase letters, and uppercase letters.



SCJP 1.4<br />SCJD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random String
 
Similar Threads
// simple Question
reading CSV file
Help me this code
problem on Math.random()
Threads