my dog learned polymorphism
The moose likes Java in General and the fly likes random letters problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "random letters problem" Watch "random letters problem" New topic

random letters problem

mark maccuish

Joined: Apr 29, 2004
Posts: 1
hey everyone,this might be a hard subject, but ill ask anyways.
I created this guessing game, 1 random number is randomly created and i get 5 chances to guess it correctly, if i do so in 5, i get 1 "bull" (point) if i dont, the computer does, and it keeps track of points. This is fairly easy and i did it no problem. HOWEVER, part B) of the program is killing me!!!
i have to now randomly generate 3 random letters (A-E) [and they cannot be the same] and guess 1 letter at a time till i get all 3. i.e. ABE is randomly created, if i guess C and hit "guess" button, i get 1 "miss" if i guess A i get a "bull" and score a point. Althought i thought this would be easy, i am having SOOOOOOO much trouble, can anyone PLEASE help me out? what would be the easiest way of approaching it? im really stuck on the validation part, i.e. after i submit the letter, how do i validate it against the "secret" random 3 letter'd word already created to see if theres a letter in it ? please help i am in dire trouble.
[removed the "HELP" from the subject, as it's somewhat redundant when asking questions in a forum - Ilja]
[ April 30, 2004: Message edited by: Ilja Preuss ]
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32507

I'm not sure what this has to do with performance, but remember letters are ordered in ASCII. So you can use the same idea as with the numbers and add the letter to 'A'. For example, 'A' + 2 = 'C'

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to Java in General (intermediate)...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think I heard you have to use A through E with no duplicates. So BDE would be good, but ABB would not.
A common approach is to "shuffle" the list. It works well for shuffling cards or generating lottery numbers. To get three random letters, try something like this:

Try this and see what kind of scrambles you get. You might shuffle more than once if you don't trust one round of swapping.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: random letters problem
It's not a secret anymore!