my dog learned polymorphism
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

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: 6529

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?
It's not a secret anymore!