Your code gets 255 because it
rounds the real number, rather than
truncating it. Rnd returns a real number on [0,1). It
can return 0, but it can
not return 1. Multiplying that by 255 converts it to a real number on [0,255). It
can be zero, but it can
not be 255. However, it can be 254.999, which your statement rounds to 255.
The problem with this is that the 256 possible integers do not have the same probability. Try this code:
Code:
Sub TestRnd()
Dim lngCounter(6) As Long
Dim i As Long
Dim sngRnd As Single, lngRnd As Long
For i = 1 To 10000
sngRnd = Rnd() * 5 'Get random real number on [0,5)
lngRnd = sngRnd 'Round it [0,5] 0 & 5 get half weight
lngRnd = Int(sngRnd) 'Truncate it [0,4] All numbers get full weight
lngCounter(lngRnd) = lngCounter(lngRnd) + 1
Next i
i = 0
End Sub
I don't know how to display an array, so I put a breakpoint on the last statement and put the array in the watch window. With your code, 0 and 255 each get half as many hits as 1-254. This is because 0.0-0.5 gets rounded to 0, 254.5-245.999 gets rounded to 255. That's only one half. The rest get rounded from -0.5 to +0.5.
With the code above, all 5 values get roughly equal tallies.
As it turns out, the font.color statement
Code:
oChr.Font.Color = RGB(sngR, sngG, sngB)
will accept floating point or fixed. Floating point are rounded. It will also accept values out of range. It converts those to 255.
Your post pointed out some other bugs in my code. I'll post a new version shortly.