Advertisement Jump to content


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


Rounding Problems in VB6

This topic is 5542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!