I've never really liked: RGB(Rnd() * 255, Rnd() * 255, Rnd() * 255) to obtain a random 32bit Color.
Therefore, I've done a NEW RndColor32 Macro (actually a #Define [single Line Macro]).
Additional explanations, like reasons and result, are below the test-code:
Code: Select all
' 32bit_Random_Color.bas, by MrSwiss - 2016-08-09
'
' play with different settings: w and h only! 640/480 | 800/600 | 1024/768
Const As Long w = 1280, h = 800, cd = 32 ' 16 x 10 = 160 squares per run
' const's to pre-calculate parameters, used in main code (based on: w and h)
Const As Long wm = w - 1, hm = h - 1
Const As Long st = w \ 80, sq = h \ 80
#Define RndColor32 ( CULng( Rnd() * (&hFFFFFFFF - &hFF000000) + &hFF000000 ) )
ScreenRes w, h, cd ' define window size and color depth
Randomize(Timer, 3) ' Mersenne Twister (-lang "FB" default), seed for Rnd()
Do
ScreenLock
For j As Long = 0 To hm Step st ' top --> bottom loop (outer loop)
For i As Long = 0 To wm Step st ' left --> right loop (inner loop)
Line (i+1, j+1)-Step(sq, sq), RndColor32, BF
Next
Next
ScreenUnLock
Sleep 199 ' 200 - 1 mS run time (approx.)
Loop Until InKey() = Chr(255, 107) ' [X] click
' using above RndColor32 Macro to obtain random color (full RGB() range)
' -----------------------------------------------------------------------
' The common way: RGB(rnd() * 255, rnd() * 255, rnd() * 255), which looks
' pretty awkward, compared to a single macro call RndColor32.
' Which is used and converted once (to ULong), instead of 3 times Rnd()
' and 3 explicit conversions, in the RGB() Macro (to UByte)!
' This means: more speed too!