Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with this code?!?!? 2

Status
Not open for further replies.

The

Programmer
Jul 30, 2001
92
CA
Dim X
Dim upperbound
Dim lowerbound
X = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
upperbound = 10
lowerbound = 1
If X = (1 Or 2 Or 3) Then
EPower = 1
End If
If X = (4 Or 5) Then
EPower = 2
End If
If X = (6 Or 7) Then
EPower = 3
End If
If X = (8 Or 9) Then
EPower = 4
End If
If X = 10 Then
EPower = 5
End If
HP = HP - EPower

---------------------

I'm trying, obviously, to make the computer randomly select a number, and subtract that number from the variable 'HP'. however it doesn't work. anybody know why? thanks
 
At first glance it looks like you need to assign values to upperbound and lowerbound before using the variables in the rnd function.
 
Ah! Thank you, it works now.
 
A case statement would probably be more efficient.


'===============
Dim X
Dim upperbound
Dim lowerbound
upperbound = 10
lowerbound = 1

X = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

select case X
case 1 Or 2 Or 3
EPower = 1
case 4 Or 5
EPower = 2
case 6 Or 7
EPower = 3
case 8 Or 9
EPower = 4
case 10
EPower = 5
end select

HP = HP - EPower
'=================
Also if you are trying to get a random number between lowerbound and upperbound including the upper and lower bounds (lowerbound <= X <= upperbound) you need to adjust your equation like:
X = Int((upperbound - lowerbound)*rnd + lowerbound)

Look at it this way:
if rnd = 0:
X = lowerbound
if rnd = 1:
X = (10 - 1)*1 + 1 = 10



Troy Williams B.Eng.
fenris@hotmail.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top