Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a function for generating RGB colors in VB6.0, and it is working properly. Please suggest improvements for this if any.

Public Function RandomRGBColor() As Long
    RandomRGBColor = RGB( _
        Int(Rnd() * 256), _
        Int(Rnd() * 256), _
        Int(Rnd() * 256))
End Function 
share|improve this question
1  
Out of interest, is there any reason why you have to use vb6? –  Pharap Aug 5 at 9:44

2 Answers 2

up vote 10 down vote accepted

You need to change/add just one little thing. You need to call the Randomize () method before using the Rnd () method.
You only should call it once, so it would be good, to call the Randomize() method inside of the constructor of your class like

Private Sub Class_Initialize()
    Randomize
End Sub

If you don't do this you will get for each iteration the same "random" number. Don't get me wrong, this doesn't mean that all numbers will be the same but assume you start your application. You will e.g get the numbers 11,54,142 for the first call of your method. If you start your application again you will get these same numbers for the first call of this method.
To prevent this, you need to seed the Rnd () method to get each time random values.

share|improve this answer
1  
While you are correct to call Randomize() it shouldn't be in this function but somewhere in the start-up of your project. It's a minor waist of resources and performance to call this every time. –  chillworld Aug 5 at 6:06
1  
@chillworld, I stated You need to call the Randomize () method before using the Rnd () method. which can be misunderstood (like you did). It hadn't be meant to call it inside of the RandomRGBColor() method. I will edit my answer. –  Heslacher Aug 5 at 6:29
    
Sorry for the misunderstaning, great you edit the answer. You have then a upvote from me –  chillworld Aug 5 at 6:34

Your code is small, clean and readable and in my eyes it is well optimised code.

If you are in need of different upper and lower bounds for your random numbers I suggest to implement the following :

Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer
   RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
End Function

It adds a little more code and slightly decreases performance, but you could use it in other parts of your project and it's more readable.

Now I do not have much experience in VB6.0 but everywhere I looked they call the Rnd() => Rnd.
I'm not sure if this is a mistake or if the compiler accepts both ways?

share|improve this answer
1  
I would expect the compiler accepts both as it does in later versions of VB. I'm not 100% certain but I believe BASIC also had optional parentheses. –  Pharap Aug 5 at 9:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.