# Random number guessing game

Page 1 of 1

## 3 Replies - 26906 Views - Last Post: 06 October 2008 - 04:59 PMRate 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=65906&amp;s=03887fe393893ce3a926216bbc9bef12&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 supaapple

Reputation: 0
• Posts: 99
• Joined: 21-February 08

# Random number guessing game

Posted 02 October 2008 - 07:09 PM

I have to make a guessing game for my class and it is supposed to choose a number at random so you can guess. This is what I have so far:
``` Dim X As Integer = Int(1000 * Rnd())

Private Sub EndToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndToolStripMenuItem.Click
End
End Sub

MessageBox.Show("Jay Mohundro version 0.0.01")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = X Then
MessageBox.Show("CONGRATULATIONS!!!")
End If
If TextBox1.Text < X Then
MessageBox.Show("You are too low!!!")
ElseIf TextBox1.Text < X - 25 Then
MessageBox.Show("You are way too low!!!")
End If
If TextBox1.Text > X Then
MessageBox.Show("You are too high!!!")
ElseIf TextBox1.Text > X + 25 Then
MessageBox.Show("You are way too high!!!")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim X As Integer = Int(1000 * Rnd())
End Sub
End Class
```

The problem is that whenever I play it the answer stays at 705. I'm new so I probably did something wrong. Any help and/or pointers are appreciated. Also if it matters I am using Microsoft Visual Basic 2008 Express Edition.

Is This A Good Question/Topic? 0

## Replies To: Random number guessing game

### #2 jacobjordan

• class Me : Perfection

Reputation: 115
• Posts: 1,499
• Joined: 11-June 08

## Re: Random number guessing game

Posted 02 October 2008 - 07:24 PM

First of all, this is VB.NET. Don't worry, a mod will move it.

Second, i know what is happening. The Rnd() method you use to generate random numbers is very predictable, it will generate the same set of numbers over and over again. So it's not you. To fix that, before you generate a random number, put Randomize(Now.Millisecond). Second, i think i see another error in your code. In the button2 click sub, i assume you are generating another random number. If so, to update the global variable X, use X = Int(1000 * Rnd()). The way you have it, it will create another variable called X, it is not updating the global one. Also, you know how i told you to use the Randomize() line to fix the random number problem? Well, when you create the global variable called X, you are already assigning it a value before you could use the Randomize line. So, replace that line with just Dim X As Integer. Then, create a load event for you form, and put this:
```Randomize(Now.Millisecond)
X = Int(1000 * Rnd())

```

That should do it. So, all in all, your class should look like this:
```Dim X As Integer

Private Sub MyBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize(Now.Millisecond)
X = Int(1000 * Rnd())
End Sub

Private Sub EndToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndToolStripMenuItem.Click
Me.Close() 'I put this in because using the End method is not the proper way to exit an application
End Sub

MessageBox.Show("Jay Mohundro version 0.0.01")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = X Then
MessageBox.Show("CONGRATULATIONS!!!")
End If
If TextBox1.Text < X Then
MessageBox.Show("You are too low!!!")
ElseIf TextBox1.Text < X - 25 Then
MessageBox.Show("You are way too low!!!")
End If
If TextBox1.Text > X Then
MessageBox.Show("You are too high!!!")
ElseIf TextBox1.Text > X + 25 Then
MessageBox.Show("You are way too high!!!")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
X = Int(1000 * Rnd())
End Sub
End Class

```

Note: to copy/paste that code without the line numbers, click the "View Plain" thing in the top of the code box.

### #3 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Random number guessing game

Posted 02 October 2008 - 08:04 PM

Moved to VB.NET

### #4 dbasnett

Reputation: 121
• Posts: 665
• Joined: 01-October 08

## Re: Random number guessing game

Posted 06 October 2008 - 04:59 PM

or the .net way

```		Dim aRandNumGen As New Random
Dim holdNumber As Integer
holdNumber = aRandNumGen.Next(1, 1001) 'generate anumber between 1 and 1000 inclusive

```