File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How do i create random strings? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do i create random strings?" Watch "How do i create random strings?" New topic
Author

How do i create random strings?

Andrew Stevens
Greenhorn

Joined: May 29, 2001
Posts: 3
I need a no paramater constructor that generates a random 4 letter string. Is there a way to do this using Math.random?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

char randomLetter = (char)('A' + (Math.random() * 26));
I'm leaving the rest for you to figure out just in case this is a school assignment
Junilu

Junilu - [How to Ask Questions] [How to Answer Questions]
Siobhan Murphy
Ranch Hand

Joined: Oct 19, 2000
Posts: 72
You could create a collection of the 26 characters, shuffle the Collection and select four elements.
Best wishes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i create random strings?