# Random Number Guessing Game

Page 1 of 1

## 6 Replies - 5595 Views - Last Post: 17 April 2012 - 06:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275382&amp;s=db4d0fd2d41b888e1dfa89e5909fa896&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kcin00

Reputation: 0
• Posts: 4
• Joined: 15-April 12

# Random Number Guessing Game

Posted 15 April 2012 - 03:51 PM

Visual Basic provides a function that can generate random numbers. This function, Rnd, will return a random number, of type Single, between 0 and 1. To get truly random numbers, you must initialize the random number generator using the Randomize statement. Place this statement in the Form Load event.

Create a program that will “think” of a number between 1 and 10. In a Do While loop, use the InputBox function to ask the user to guess the number. Keep track of how many tries it takes the user to guess the number.

Tell the user how many tries it took him to guess the number. Use Select Case to display an appropriate message, based on the number of tries (see below). You may use a label on the form or MsgBox for the output.

To create a random number between 1 and 10, multiply the result of Rnd by 10 and add 1. Rnd * 10 + 1
Use the Int function to convert the result of Rnd to an integer.

Here’s help (use Option Explicit, Val function, appropriate data types and standard prefixes):

Private Sub cmdStart_click( )

‘Declare RandomNumber variable

‘Declare InputNumber variable

‘Declare Counter variable

‘Create random number between 1 and 10
RandomNumber = Int(Rnd * 10 + 1)

‘Use this line of code during testing, then remove it!
Print RandomNumber

‘Get numeric value of user’s guess and accumulate number of guesses until there’s a match
Do While InputNumber <> RandomNumber
InputNumber = Val(InputBox (“Guess my number between 1 and 10”,”Guessing Game”))
Counter = Counter + 1
Loop

‘Display output
Select Case Counter
Case 1
MsgBox (“Wow! You guessed my number in 1 try!”)
Case 2 to 3
MsgBox (“Great job! You guessed my number in “ & Counter & “ tries!”)
Case 4 to 5
.
.
.
End Select
End Sub

Every time that I press the start button to begin guessing numbers, the answer is revealed on the side. How do I stop this?

Is This A Good Question/Topic? 0

## Replies To: Random Number Guessing Game

### #2 Alderdragon

Reputation: 2
• Posts: 50
• Joined: 06-November 09

## Re: Random Number Guessing Game

Posted 15 April 2012 - 03:59 PM

I've never used VB, but are you using some type of control to show the answer on the side? If so, check that control's properties for a visibility property. You can toggle it on and off appropriately.

### #3 kcin00

Reputation: 0
• Posts: 4
• Joined: 15-April 12

## Re: Random Number Guessing Game

Posted 15 April 2012 - 04:10 PM

Alderdragon, on 15 April 2012 - 03:59 PM, said:

I've never used VB, but are you using some type of control to show the answer on the side? If so, check that control's properties for a visibility property. You can toggle it on and off appropriately.

I appreciate the help but I am not using a control to show the answer. I have been messing around with visibility properties but just can't seem to figure it out.

### #4 kcin00

Reputation: 0
• Posts: 4
• Joined: 15-April 12

## Re: Random Number Guessing Game

Posted 15 April 2012 - 04:49 PM

Even more bizarre, when i drag the input box over the answer that is being displayed it magically disappears

### #5 kcin00

Reputation: 0
• Posts: 4
• Joined: 15-April 12

## Re: Random Number Guessing Game

Posted 15 April 2012 - 05:02 PM

```Private Sub cmdStart_Click()

'Declare RandomNumber varible

'Declare InputNumber varible

'Declare Counter varible

'Create random number between 1 and 10
RandomNumber = Int(Rnd * 10 + 1

'Use this line of code during testing, then remove it!
Print RandomNumber

'Get numeric value of user's guess and accumulate number of guesses until there's a match
Do While InputNumber <> RandomNumber
InputNumber = Val(InputBox("Guess my number between 1 and 10", "Guessing Game"))
Counter = Counter + 1
Loop

'Display Output
Select Case Counter
Case 1
MsgBox ("Wow! You guessed my number in 1 try!")
Case 2 To 3
MsgBox ("Great job! You guessed my number in " & Counter & " tries!")
Case 4 To 5
MsgBox ("Good job. You guessed my number in " & Counter & " tries.")
Case 6 To 9
MsgBox ("Ouch! You guessed my number in " & Counter & " tries.")
Case 10
MsgBox ("Really? You guessed my number in " & Counter & " tries.")
End Select
End Sub

Dim RandomNumber As Integer
Randomize 'Initialize random-number generator.
'Generate random number between 1 and 10
RandomNumber = Int((Rnd * 10) + 1)

End Sub

```

Can someone tell me if I'm on the right track?

### #6 maj3091

• D.I.C Lover

Reputation: 314
• Posts: 1,856
• Joined: 26-March 09

## Re: Random Number Guessing Game

Posted 15 April 2012 - 11:45 PM

You have a line that says Print RandomNumber, which is printing your answer to screen.

If you read the code you posted originally, there is even a comment above it telling you to remove it!

```‘Use this line of code during testing, then remove it!
Print RandomNumber
```

### #7 BobRodes

• Lovable Curmudgeon

Reputation: 588
• Posts: 3,036
• Joined: 19-May 09

## Re: Random Number Guessing Game

Posted 17 April 2012 - 06:40 AM

maj beats me to it again. A prof that went to this much trouble to hold your hand through the process of writing your first simple program is going to feel hurt that you don't even read his stuff. If you go to the trouble of actually studying the example given, you'll find some good techniques you can use to keep yourself on track while writing programs. This Print statement is one of them: it helps you make sure that you've done your random number generation correctly.

Just so you know, the Print statement prints directly on the form. If you drag something over it, well, that thing is on top of it. That's why it disappears. it's analogous to painting a logo on your desk and putting a piece of paper on top of it.