Archived

This topic is now archived and is closed to further replies.

gamechampionx

Rounding Problems in VB6

Recommended Posts

In VB6, I''m using the line: random_num = (Rnd * (enemy_muscle * 10 + 10) + 1) \ 1 to randomize a number from 1 to 10 times the enemy_muscle variable. The problem occurrs when Rnd is something like 0.99... What happens is that the compiler rounds it up to 10, so results are one more than they should be. How can I fix this?

Share this post


Link to post
Share on other sites
I used this (straight from my old VB DX engine):

Public Function RandomNumber(ByVal Min As Long, ByVal Max As Long, Optional ByVal RandomizeCounterStart = 1000) As Long
' Returns a random number between the Min and Max values.
' Every thousand random numbers, it randomizes the random number generator.
Static RandomizeCounter As Integer

RandomizeCounter = RandomizeCounter - 1
If RandomizeCounter <= 0 Then
Randomize Timer
RandomizeCounter = RandomizeCounterStart
End If

RandomNumber = Int(Rnd * ((Max + 1) - Min)) + Min
End Function


[EDIT] You don't even have to randomize the random number generator when your program starts if you use that.

[edited by - Rob Loach on November 16, 2003 12:13:47 AM]

Share this post


Link to post
Share on other sites