Random Number Generate (within range)

tt1611

Member
Joined
Nov 30, 2009
Messages
9
Programming Experience
Beginner
Good Afternoon. First time poster on this forum and i am quite new to VB.Net
I have the following scenario.

The object of this project is to write a computer program that will allow the
computer to guess a number that you have selected. The computer will make a
guess and you, the player, will tell the computer if it guessed too high, too low, or
that it guessed the number.
The program will also allow the player to start a new game or to exit the program.
PROJECT INSTRUCTIONS:
1. Create a form with a labeled box to display the computers guess.
2. Create a new game and an exit button.
3. Create objects to indicate if the guess was too high, too low, or right on target.
4. Create program code to respond to the player’s indication by selecting and
displaying a new computer guess.
5. When the computer guesses the player’s number, reset the so that a new
game may begin.



I have done all as above and so far my code looks like so

VB.NET:
Public Class Form1
    Dim Guess As New Random()
    Dim HGuess As Integer, LGuess As Integer, current As Integer

    Private Sub cmdhigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdhigh.Click
        Dim G As Integer = Guess.Next(1, 100)
        current = Integer.Parse(Me.txtguess.Text)

        HGuess = G > current
        LGuess = G < current


        Me.txtguess.Text(Guess.Next(LGuess, HGuess)).ToString()

    End Sub

    Private Sub cmdnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdnew.Click

        MsgBox("My First Guess")
        Me.txtguess.Text = "50"

    End Sub

    Private Sub cmdlow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlow.Click

        current = Me.txtguess.Text
        HGuess = (Guess.Next(current, 101))
        LGuess = (Guess.Next(1, current))

        Me.txtguess.Text = Integer.Parse(Guess.Next(1, HGuess)).ToString


    End Sub
End Class

The problem i ran into is that if for example a user has a number 24, when the program makes an initial guess of 50,i want to be able to click low, and the program should generate a lower number say example 22. If i then click higher as my actual number uis 24, the program should not shoot higher than a number it has previously guess incorrect (in this case 50) it should guess anywhere from 22 - 49 and in turn limit the range of the next consecutive guesses it till hits the right number. My code right is guessing as it shuold but not within the limited ranges.

I have done my best to explain this but if anything is still unclear, please let me know and i will respond accordingly

Thanks
 
The first guess should be a random number within the range of 1-101. (You need 1 more than the actual range after the Next().) Do not use "50."

In the High event, use an If block and assign the new random range to a number between 1 and the current guess.

In the Low event, use an If block and assign the new random range to a number between the current guess + 1 and 101.
 
Hi Solitaire
Thanks for getting back. The catch to this is i need the game to always start with a guess of 50. From there, we specify whether the guess is too high or low. Therefore my guess.next cannot be in the range 1 -101 range specified. Only future guesses should fall within this range.
 
This looks an awful lot like a homework assignment...

That said, this is a simple binary selection algorithm. You didn't say it, but I assume the range is 1 to 100, but no matter what the range is, your first guess is at the mid point between the low and the high. When you know that that guess is either high, low or correct, you make your second guess at the mid point between your new known low and high.

So, keep track of your known lower boundary and your known upper boundary and always "guess" the middle...

Guess = (high-low) \ 2 ' Note this returns only the integer portion of the division

You will have to work the code to assure you can hit the boundaries.

Does that help?
 
Mr Cenzo

I redesigned my code. And yes you were right. This is a simple binary search for a range of numbers between 1 and 100

My code now looks like the below

Public Class Form1
Dim Guess As New Random()
Dim G As Integer, HGuess As Integer, LGuess As Integer

Private Sub cmdhigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdhigh.Click
'TOO LOW

LGuess = G

If HGuess = 0 Then
Me.txtguess.Text = (Guess.Next(LGuess, 101)).ToString
G = Integer.Parse(Me.txtguess.Text)

Else
Me.txtguess.Text = (Guess.Next(LGuess, (HGuess + 1))).ToString
G = Integer.Parse(Me.txtguess.Text)
End If

End Sub

Private Sub cmdnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdnew.Click

MsgBox("My First Guess")
Me.txtguess.Text = (Guess.Next(1, 101)).ToString
G = Integer.Parse(Me.txtguess.Text)


End Sub

Private Sub cmdlow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlow.Click

'TOO HIGH

Me.txtguess.Text = (Guess.Next(1, 101)).ToString
HGuess = G

If LGuess = 0 Then
Me.txtguess.Text = (Guess.Next(1, (HGuess+ 1))).ToString
G = Integer.Parse(Me.txtguess.Text)

Else
Me.txtguess.Text = (Guess.Next(LGuess, HGuess)).ToString
G = Integer.Parse(Me.txtguess.Text)
End If

End Sub

Private Sub cmdsolve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsolve.Click
Me.txtguess.ForeColor = Color.Red

MessageBox.Show("I WIN")

End Sub

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

End Class

I got this to work somewhat ok bar the requirement for the first guess to be 50. This i was told involves using static declarations.

I tried declaring my initial guess of 50 as Static as this was i was going to use to program the other guesses but for some reason i cant seem to be able to do this in VB.Net

I tried
Static Current as Integer
and just got an error under static asking me to use Dim instead

So now the above code works fine but will not start the game with the initial guess as 50. Rather it just "guesses a random number and i narrow down the choices from there. I will try using ur idea of ((high-low)/2) and see if that works out with this construct


Thanks for coming back anyway.
 
Why dont you try creating a boolean of firstguess and set it to true at the beginning, then do an if statement detecting of that is true, if it is, make it guess 50, then set the boolean to false so it wont guess 50 again? Or am I missing something? That seems like the easiest way to get your 50 the first time.
 
Back
Top