# [Q] Generate Random Letter

• 01-30-2011, 08:10 PM
iriscience
[SOLVED] Generate Random Letter
Hi! I am very new to Java. This is my first time taking a class on it and for an assignment I have to make a game that generates a random number and has the player guess it within 4 tries. Unfortunately, I cannot figure out how to generate a random letter. I have been searching for around an hour and cannot figure it out! From what I have gathered, I have make a string containing my letters.

Code:

`String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};`
Then, I am supposed to generate a random number from 0-25.

Code:

`int num = (int)(Math.random() * 26);`
Then, I am supposed to assign the randomly generated number to it's letter equivalent. And this is where I'm lost. I have no idea where to go from this point. I can't seem to figure out what code I can use to assign the number to it's corresponding letter. Any help is GREATLY appreciated! Thanks so much!

PS: I'm sorry if this has been a recurring question. I have looked in so many forums and read so many different threads, that my brain feels all squishy. So please don't hate me too much for this.
• 01-30-2011, 08:28 PM
eRaaaa
letters[num] :confused:
• 01-30-2011, 08:30 PM
Vase
The number you generated is between 0 and 25, which is perfect, because that corresponds to the elements in the array you created.

Say the number you generated is 4, and you want this to be converted to "E" (where 0 is "A").

Code:

`String choice = letters[num];`
will assign the letter at element [num] to the choice. Generate a 3, you have "D". Generate a 25, you have "Z".
• 01-30-2011, 08:31 PM
pbrockway2
Code:

```String[] letters = ... int num = ... String randomLetter = letters[num];```

The expression letters[num] means "the n-th element of the letters array". See Arrays in Oracle's Tutorial, or your textbook.

--------------------------

"Just the place for a Snark!" the Bellman cried,
As he landed his crew with care;
Supporting each man on the top of the tide
By a finger entwined in his hair.

"Just the place for a Snark! I have said it twice:
That alone should encourage the crew.
Just the place for a Snark! I have said it thrice:
What i tell you three times is true."
• 01-30-2011, 08:31 PM
Zman3359
Here is a pretty easy way to do it. Have the String[] with all the letters then get one of them using a randomly generated number from 0 to 25. Like this:
Code:

```String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};   public static String randomLetter()   {       int num = (int)(Math.random() * 26);       return letters[num];   }```
• 01-30-2011, 08:36 PM
pbrockway2
Two more and I get to quote A. A. Milne...
• 01-30-2011, 08:48 PM
iriscience
Quote:

Originally Posted by pbrockway2
Code:

```String[] letters = ... int num = ... String randomLetter = letters[num];```

Thank you so much! This is exactly what I was looking for! Couldn't have been easier! Thank you! Thank you! :D
• 01-30-2011, 09:55 PM
Fubarable
Quote:

Originally Posted by pbrockway2
Two more and I get to quote A. A. Milne...

My favorite:
"Did you ever stop to think, and forget to start again?"
• 01-30-2011, 11:25 PM
pbrockway2
Quote:

Thank you so much!
You're welcome.
• 01-30-2011, 11:27 PM
Fubarable
OK, it's killing me, and sorry to be off topic, but what A. A. Milne quote were you talking about??
• 01-31-2011, 12:14 AM
Junky
This can be achieved without using an array for the letters. A hint:
Code:

`System.out.println((char)65);`
Now all you need is to generate a random number in the correct range.
• 01-31-2011, 01:10 AM
pbrockway2
Quote:

but what A. A. Milne quote were you talking about?

Well I was the third person to suggest accessing the array at the index that had been calculated. Hence the Snark quote.

Then Zman3359 suggested the same thing (4).

And I thought "Two more and ... Now We Are Six"

------------------

When I was One,
When I was Two,
I was nearly new.
When I was Three
I was hardly me.
When I was Four,
I was not much more.
When I was Five,
I was just alive.
But now I am Six,
I'm as clever as clever,
So I think I'll be six now for ever and ever.