View Single Post
  #15   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default Bias in rand for excel 07

Ah, so that is how you draw into a UserForm... ThunderDFrame... I'll have to
remember that.

Thanks for performing the conversion. First, I removed the "1" off of the
Randomize statement in the second loop so that both Randomize statements
draw from the Timer for their seed values. Yes, this scatters the bar effect
on the second picture, but I think it is a fairer display of what is going
on. Second, I think if you change the Const C assignment statement in the
PaintPixel procedure to this...

Const C As Long = 16711680

you get a much starker (and more amazing) contrast between the two displays,
especially with the the Randomizer seed change mention above, especially
when you repeatedly click the UserForm. Yes, this removes the sharp vertical
lines that the seed value of 1 produced, but it still shows (and more fairly
I think) the sharp difference in randomization that occurs if you use the
Randomize statement only once. The modified code implementing the above is
shown below my signature.

Again, thanks for performing the compiled VB to Excel VBA conversion... you
did a nice job with it. I will be using your coded translation (with
appropriate acknowledgement to you, of course) should a similar question
rise in the future.

Rick

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetPixel Lib "gdi32" _
(ByVal hDc As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal crColor As Long) As Long

Private Declare Function GetPixel Lib "gdi32" _
(ByVal hDc As Long, _
ByVal x As Long, _
ByVal y As Long) As Long

Private Declare Function GetDC Lib "user32" ( _
ByVal hWnd As Long) As Long

Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hWnd As Long, ByVal hDc As Long) As Long

Private Sub PaintPixels()
Dim hWnd As Long, hDc As Long
Dim tp As Long, lt As Long
Dim x As Long, y As Long
Dim colr As Long

Const Z As Long = 128 * 2 - 1
Const C As Long = 16711680

Me.Left = 10: Me.Top = 10
Me.Width = (Z * 2) * 0.75 + 45: Me.Height = Z * 0.75 + 60
' if form is too small, change 0.75 to 1 or 1.25

hWnd = FindWindow("ThunderDFrame", Me.Caption)
hDc = GetDC(hWnd)

tp = Me.Top + 15
lt = Me.Left + 10

Randomize
For y = tp To tp + Z
For x = lt To lt + Z
colr = Rnd * C
SetPixel hDc, x, y, colr
Next
Next

lt = lt + Z + 15
For y = tp To tp + Z
For x = lt To lt + Z
Randomize
colr = Rnd * C
SetPixel hDc, x, y, colr
Next
Next

ReleaseDC hWnd, hDc
End Sub

Private Sub UserForm_Activate()
Me.Caption = "Click me to (re-) PaintPixels"
PaintPixels
End Sub

Private Sub UserForm_Click()
' Me.Repaint
PaintPixels
End Sub