File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do i create random strings?" Watch "How do i create random strings?" New topic

How do i create random strings?

Andrew Stevens

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

Joined: Feb 26, 2001
Posts: 5471

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 - [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:
subject: How do i create random strings?