hi guys, writing a code in my first term at vb 2010, ive to create a guessing game, generate a number between 1 and 30 and let the user guess it. i must give hints along the way and record the amount of guesses made. i have the code written and working. my problem lays with changing the range. if i change the range to any other number, when i press enter it keeps givin a new random number each time.
Public Class frmMain

Public min As Integer
Public max As Integer
Public randomClass As New Random
Public num As Integer

Public X As Integer


'Dim X As Integer = Int(X * Rnd())

Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult


Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    min = 1
    max = 30

    Randomize()
    X = randomClass.Next(min, max)
    'X = CInt(Int(30 * Rnd()))
    lblMin.Text = min.ToString
    lblMax.Text = max.ToString
    For i As Integer = min To max
        ComboBox1.Items.Add(i)
    Next
End Sub

Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    End
End Sub

Private Sub BackgroundColourToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BackgroundColourToolStripMenuItem.Click

End Sub



Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click


    txtAttempts.Text = Val(txtAttempts.Text) + 1


    num = ComboBox1.Text
    Label2.Text = X

    If ComboBox1.SelectedIndex = X Then
        MsgBox("Congratulations!!!")
        msg = "  do you want to Exit"
        title = "MsgBox Close"

        style = MsgBoxStyle.Critical Or MsgBoxStyle.YesNoCancel
        response = MsgBox(msg, style, title)

        If response = MsgBoxResult.Yes Then 'user choose yes
            'perform some action
            MsgBox("THANK YOU FOR PLAYING THE GUESSING GAME!!")
            End
        ElseIf MsgBoxResult.No Then 'user choose no

            txtGameTotal.Text = Val(txtGameTotal.Text) + 1

            txtAverage.Text = "Your Average is " & txtAttempts.Text / txtGameTotal.Text
            MsgBox("Your Average Guess Total is " & txtAttempts.Text / txtGameTotal.Text)

            X = Int(X * Rnd())
            txtEntry.Clear()
            txtAttempts.Clear()

            Return
        ElseIf MsgBoxResult.Cancel Then  'user choose cancel
            'perform some action
            txtGameTotal.Text = Val(txtGameTotal.Text) + 1
            txtAverage.Text = "Your Average is " & txtAttempts.Text / txtGameTotal.Text
            MsgBox("Your Average Guess Total is " & txtAttempts.Text / txtGameTotal.Text)
            txtEntry.Clear()
            txtAttempts.Clear()

            Return
        End If




        X = Int(X * Rnd())

    End If
    If num < X Then
        txtEntry.Text = ("You are too low!!!")

    End If
    If num > X Then
        txtEntry.Text = ("You are too high!!!")


    End If


End Sub

Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
    MsgBox("THANK YOU FOR PLAYING THE GUESSING GAME!!")

    End
End Sub


Private Sub LightBlueToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles LightBlueToolStripMenuItem1.Click
    Me.BackColor = Color.LightBlue

End Sub

Private Sub GreenToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles GreenToolStripMenuItem1.Click
    Me.BackColor = Color.Green

End Sub

Private Sub BeigeToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles BeigeToolStripMenuItem1.Click
    Me.BackColor = Color.Beige

End Sub

Private Sub CrimsonToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles CrimsonToolStripMenuItem1.Click
    Me.BackColor = Color.Crimson

End Sub

Private Sub RangeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RangeToolStripMenuItem.Click
    frmRange.Show()
End Sub



Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

End Sub

Private Sub btnRandom_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click


    'Randomize()
    'X = randomClass.Next(min, max)

    lblMin.Text = min.ToString
    lblMax.Text = max.ToString
    TextBox1.Text = X

    txtAttempts.Text = Val(txtAttempts.Text) + 1


    num = ComboBox1.Text
    Label2.Text = X

    If ComboBox1.Text = X Then
        MsgBox("Congratulations!!!")
                msg = "  do you want to Exit"
                title = "MsgBox Close"

                style = MsgBoxStyle.Critical Or MsgBoxStyle.YesNoCancel
                response = MsgBox(msg, style, title)

                If response = MsgBoxResult.Yes Then 'user choose yes
                    'perform some action
                    MsgBox("THANK YOU FOR PLAYING THE GUESSING GAME!!")
                    End
                ElseIf MsgBoxResult.No Then 'user choose no

                   txtGameTotal.Text = Val(txtGameTotal.Text) + 1

                    txtAverage.Text = "Your Average is " & txtAttempts.Text / txtGameTotal.Text
                    MsgBox("Your Average Guess Total is " & txtAttempts.Text / txtGameTotal.Text)

                    X = Int(X * Rnd())
                    txtEntry.Clear()
                    txtAttempts.Clear()

                    Return
                ElseIf MsgBoxResult.Cancel Then  'user choose cancel
                    'perform some action
                   txtGameTotal.Text = Val(txtGameTotal.Text) + 1
                   txtAverage.Text = "Your Average is " & txtAttempts.Text / txtGameTotal.Text
                   MsgBox("Your Average Guess Total is " & txtAttempts.Text / txtGameTotal.Text)
                   txtEntry.Clear()
                   txtAttempts.Clear()

        '            Return
    End If




    '        X = Int(X * Rnd())

    '    End If
    '    If num < X Then
    '        txtEntry.Text = ("You are too low!!!")

    '    End If
    '    If num > X Then
    '        txtEntry.Text = ("You are too high!!!")


    '    End If



End Sub

End Class

Dani AI

Generated

Good that got it working. The two common, subtle causes in the posted code were: (1) comparing the ComboBox's SelectedIndex (zero‑based) to the secret number, and (2) mixing legacy Rnd/Randomize calls with a System.Random instance and accidentally reassigning the secret number during play. Both lead to off‑by‑one or continually changing targets.

A simple, reliable pattern:

  • Keep one Random instance at class scope and only generate the secret when a new game (or range change) starts.
  • Parse the chosen value from the ComboBox (or use SelectedItem) and compare that value to the secret — do not compare SelectedIndex unless the index-to-value mapping is explicit.
  • Use Integer.TryParse to avoid implicit conversions and crashes.
  • Remember Random.Next(min, max) treats max as exclusive, so use max + 1 to include the upper bound.

Example (minimal, focused on the fixes):

' class-level
Private rnd As New Random()
Private target As Integer
Private minValue As Integer = 1
Private maxValue As Integer = 30
Private attempts As Integer = 0

' start/reset game
target = rnd.Next(minValue, maxValue + 1)
attempts = 0

' on guess
Dim guess As Integer
If Integer.TryParse(ComboBox1.Text, guess) Then
  attempts += 1
  If guess = target Then
    ' record win, then generate a new target only when starting the next round
    target = rnd.Next(minValue, maxValue + 1)
    attempts = 0
  ElseIf guess < target Then
    txtEntry.Text = "Too low"
  Else
    txtEntry.Text = "Too high"
  End If
End If

Extra tips: repopulate the ComboBox and reset SelectedIndex when the range changes; avoid calling Randomize/Rnd together with System.Random; enable Option Strict On to catch implicit conversions; only increment attempts for valid guesses. These steps prevent surprising target changes and the usual off‑by‑one comparisons.

sorted myself

num = ComboBox1.Text
        Label2.Text = X

        If ComboBox1.SelectedIndex = X Then



    i should have had num = X.  i wanted to get the principals of the game working before i ironed out the small issues
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.