Hi there,
Couple of things I don't understand.
These variables..
Code:
Dim i, t, a1, b2 As Integer
.. all EXCEPT
b2 will be dimensioned as Variant types. You must specify EACH variable and their types. So that should become...
Code:
Dim i as long, t as long, a1 as long, b2 As Integer
Also, there is little to no need for declaring Integer types. When VBA declares a variable (or compiles rather) to an Integer type, it first converts it to Long, then shortens it up and converts it again. And since Long types can hold everything an Integer type can plus more, there is almost no need to even declare a variable Integer type. Plus Long is shorter to type.
I do not understand your logic. What do you want your
seed to be? You want a time value of ss:mm seconds in the minute slot and minutes in the second slot? Shall it be a time value or string value? What are you after with that? From what I understood you would be looking at something like this ..
Code:
seed = TimeValue("00:" & Format(Second(Time), "00") & ":" & Format(Minute(Time), "00"))
And if you want it a time value, declare your variable as the Date type instead of Integer.
Plus there are a few variables that you just don't need, which means you could trim your overall code down quite a bit. For example, there is no need to set your font color at the beginning and then again after each iterative Do/Loop, it's just a waste of time. With the above stated, maybe you could look at the following...
Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Random()
Dim seed As Date
Dim i, t, a1, b2 As Integer
Range("A1:C1").Font.ColorIndex = 3
Range("A1:C1").Font.Bold = True
seed = TimeValue("00:" & Format(Second(Time), "00") & ":" & Format(Minute(Time), "00"))
Randomize (seed)
t = 1
Do
t = t + 5: Sleep t
ActiveSheet.Cells(1, 1).Value = Int(2 * Rnd)
Loop While t < 20
seed = TimeValue("00:" & Format(Second(Time), "00") & ":" & Format(Minute(Time), "00"))
Randomize (seed)
t = 1
Do
t = t + 5: Sleep t
ActiveSheet.Cells(1, 2).Value = Int(10 * Rnd)
Loop While t < 20
seed = TimeValue("00:" & Format(Second(Time), "00") & ":" & Format(Minute(Time), "00"))
Randomize (seed)
t = 1
Do
t = t + 5: Sleep t
ActiveSheet.Cells(1, 3).Value = Int(10 * Rnd)
Loop While t < 20
End Sub
Not sure why you would want to use the
Sleep API unless it was for visual effect only.
As far as randomization goes, the Rnd() function is based on a human programmed algorithm. This means that it is never truly and wholely random. If somebody knew the algorithm, or spent time disecting it, one could predict the outcome every time. Nothing in computers is ever random, because they only do what you tell it to do. Only humans can attempt to do true random findings. And them, sometimes the so-called earnest findings are indeed random as well.
