Thread: [Q] Generate Random Letter

1. Member
Join Date
Jan 2011
Location
Ohio
Posts
11
Rep Power
0

[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.

Java 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.

Java 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.
Last edited by iriscience; 01-31-2011 at 07:28 AM. Reason: solved

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
letters[num] :confused:

3. Member
Join Date
Jan 2011
Location
Gainesville, FL
Posts
45
Rep Power
0
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").

Java 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".

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Java 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."

5. 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:
Java 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];
}```

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Two more and I get to quote A. A. Milne...

7. Member
Join Date
Jan 2011
Location
Ohio
Posts
11
Rep Power
0
Originally Posted by pbrockway2
Java 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

8. 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?"

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Thank you so much!
You're welcome.

10. This can be achieved without using an array for the letters. A hint:
Java Code:
`System.out.println((char)65);`
Now all you need is to generate a random number in the correct range.

11. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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,
I had just begun.
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.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•