3 Replies - 22129 Views - Last Post: 06 October 2008 - 04:59 PM Rate Topic: -----

#1 supaapple  Icon User is offline

  • D.I.C Head

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

Random number guessing game

Post icon  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

	Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
		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  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • 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

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        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.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Random number guessing game

Posted 02 October 2008 - 08:04 PM

Moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • 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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1