|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
VeriSign Code Signing Digital Certificates provides assurance to end users. Read about this and more in the free white paper: “How to Digitally Sign Downloadable Code for Secure Content Transfer.” Learn More! |
|
#1
|
|||
|
|||
|
Random Game, trouble with random.
Hi, we have an assignment at school to create a game which generates a random number and you have to guess what the number is from 1 - 100. The teacher said we had to have hte number in general, something about the program always choosing the same numbers if it was on a button. ifinally figured out how to put it in the general section, and it only picks the same number in the beginning. Everytime you start the game, the number is always either 0 or 1 and most of the time it will stay 0 or 1 until you pick either one of those. Here's the codE:
Option Explicit ' Declares variables holding highest and lowest numbers Dim HIRAN As Integer Dim LOWRAN As Integer ' Declares the variable holding the input number Dim INPUTNUMBER As Integer ' Declares the variable holding the Random number Dim RANNUM As Integer ' Declares the variables holding the strings ' That appear when you enter a guess Dim CORRECT As String Dim TOOHIGH As String Dim TOOLOW As String Dim WRONGNUM As String Dim SECONDWRONGNUM As String Function Random() Randomize RANNUM = ((HIRAN - LOWRAN + 1) * Rnd + LOWRAN) End Function Public Sub CmdCheck_Click() ' Makes the highest number 99 and lowest number 1 HIRAN = 100 LOWRAN = 1 ' Changes the variables to the statements that ' appear as hints CORRECT = "CORRECT!" TOOHIGH = "High" TOOLOW = "Low" WRONGNUM = "That's higher than 99!" SECONDWRONGNUM = "That's Lower than 1..." ' Makes the variable INPUTNUMBER hold the number ' entered by the user INPUTNUMBER = txtInput.Text ' Displays a hint saying that the entered number ' is higher than 100 If INPUTNUMBER > 100 Then MsgBox (WRONGNUM) ' Displays a hint saying that the entered number ' is lower than 1 ElseIf INPUTNUMBER < 1 Then MsgBox (SECONDWRONGNUM) ' Informs the user they entered the correct number ' when the number they entered is equal to ' the random number ElseIf INPUTNUMBER = RANNUM Then lblHiLow.Caption = CORRECT ' Informs the user when they enter a number that ' is higher than the random number ElseIf INPUTNUMBER > RANNUM Then lblHiLow.Caption = TOOHIGH ' Informs the user when they enter a number that ' is lower than the random number ElseIf INPUTNUMBER < RANNUM Then lblHiLow.Caption = TOOLOW End If End Sub ' Ends the program when Quit button is clicked Private Sub CmdQuit_Click() Unload Me End End Sub ' Starts the Function Random Private Sub CmdStartgame_Click() Call Random CmdCheck.Visible = True lblHiLow.Visible = True lblInstructions.Visible = True txtInput.Visible = True Print RANNUM End Sub So basically I just need help to make it start off with a truely random number rather than 1 or 0. what I did is I made a button to make the computer choose a new number everytime the button is clicked, and a seperate button to change the variable holding the Input number. I'm also having a problem changing the color of a command button, I changed every possible drop down color changing menu, but nothing happens at all, and I"m running vb 5 CCE. Thanks |
|
#2
|
||||
|
||||
|
I made a simple application to test and it worked fine with the example listed in the msdn library.
Code:
CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)) Code:
Option Explicit
Private Const MAX = 100
Private Const MIN = 1
Private iKey As Integer
Private lTries As Long
Private Function Random() As Integer
Random = CInt(Int((MAX - MIN + 1) * Rnd() + MIN))
End Function
Private Sub cmdGuess_Click()
Dim lGuess As Long
lTries = lTries + 1
lGuess = CInt(txtGuess)
If (lGuess < iKey) Then
lblResult = "Try # " & lTries & " :Too low."
ElseIf (lGuess > iKey) Then
lblResult = "Try # " & lTries & " Too high."
Else
'they match if they get here
lblResult = "Try # " & lTries & " Correct!"
iKey = Random
lTries = 0
End If
End Sub
Private Sub Form_Load()
Randomize
iKey = Random
lTries = 0
End Sub
|
|
#3
|
|||
|
|||
|
Random number Test:
Private sub command1_click() Const MAX = 100 Const MIN = 1 Dim MyValue, Response Randomize Do Until Response = vbNo MyValue = Int((MAX * Rnd) + MIN) ' Generate random number between 1 to 100 MsgBox MyValue Response = MsgBox ("Roll again? ", vbYesNo) Loop end sub |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Random Game, trouble with random. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|