#### Archived

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

# Rounding Problems in VB6

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

## 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 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)) + MinEnd 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]