aspose file tools
The moose likes Java in General and the fly likes Generating random Strings. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generating random Strings." Watch "Generating random Strings." New topic
Author

Generating random Strings.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 361

Hi All,

I want to generate 1800000 random Strings (can be any thing like abc or cba) but this should be unique. Any idea how to achieve this using Java.



Pawan Chopra
SCJP - DuMmIeS mInD
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, you're not going to get 1.8 million unique three-lowercase-character strings, that's for sure.

There are a lot of ways to do this; here's one: build random strings and put them into a set until its length is 1800000.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
3**26 -- the number of 3 letter combinations -- is bigger than 1.8 million.

Is being "random" what's important? Or do they just need to be unique? If you need 1.8 million unique strings then you could simply use the values "1" through "1800000". Obviously, those won't be random. If you want characters rather than numbers, look into BigInteger using a radix of 26, or code something yourself. You could also look into class java.util.UUID, although I doubt those are designed to be random, per se.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

Max Rahder wrote:3**26 -- the number of 3 letter combinations -- is bigger than 1.8 million.

Yes it is. But the number of distinct three letter combinations is not 3^26 - it's 26^3 (or 17576):
- 26 possibilities for the first letter
- another 26 for the second
- another 26 for the third

You would get 3^26 with 26 "fields" each with 3 options.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Rob Prime wrote:
Max Rahder wrote:3**26 -- the number of 3 letter combinations -- is bigger than 1.8 million.

Yes it is. But the number of distinct three letter combinations is not 3^26 - it's 26^3 (or 17576):
- 26 possibilities for the first letter
- another 26 for the second
- another 26 for the third

You would get 3^26 with 26 "fields" each with 3 options.


Duh, whoops, yeah, you're right of course! Got my "powers of" mixed up!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Generating random Strings.
 
Similar Threads
WA #1.....word association
Generate random strings
generation of random numbers without using Random
how to get a random alphabet or char ?
how to Initialize an ArrayList in a constructor with no parameters?