Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old September 15th, 2003, 06:35 PM
Halofreak117 Halofreak117 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 2 Halofreak117 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 15th, 2003, 09:58 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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))
Here is the total code I used for the example:
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
This was done with vb6 sp5

Reply With Quote
  #3  
Old September 16th, 2003, 12:00 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via MSN to cleverpig
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Random Game, trouble with random.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway