pcc
Well-known Member
- Joined
- Jan 21, 2003
- Messages
- 1,382
- Office Version
- 2021
- Platform
- Windows
Can anyone explain this behaviour? I have a procedure that randomly generates row numbers and column numbers in the range 1 to 80.
Once the row number and column number are generated, the relevant cell is coloured black. This is repeated until all cells in the range are black,
and the time taken, plus number of attempts to select all 6400 (ie 80x80) cells is written to a log sheet. This process is then repeated 18 times. (NB this is for amusement only -
it's not used for anything useful, just curiosity).
On running the procedure, I find that it typically takes 20 to 30 seconds, with fewer than around 100,000 attempts. However, as seen from the log sheet, it sometimes takes far longer to run (see attempts 4, 9 and 10).
Logically I would expect a variation in the times, but there is such a variation from max to min that it looks like the data is not entirely random. What causes this?
Once the row number and column number are generated, the relevant cell is coloured black. This is repeated until all cells in the range are black,
and the time taken, plus number of attempts to select all 6400 (ie 80x80) cells is written to a log sheet. This process is then repeated 18 times. (NB this is for amusement only -
it's not used for anything useful, just curiosity).
On running the procedure, I find that it typically takes 20 to 30 seconds, with fewer than around 100,000 attempts. However, as seen from the log sheet, it sometimes takes far longer to run (see attempts 4, 9 and 10).
Logically I would expect a variation in the times, but there is such a variation from max to min that it looks like the data is not entirely random. What causes this?
VBA Code:
Sub pixels()
Application.ScreenUpdating = False
Sheets.Add.Name = "Log"
[a1] = "Duration"
[b1] = "Tries"
drow = 2
ncols = 80
nrows = 80
Sheets("Main").Activate
Application.ScreenUpdating = True
iter = 1
Do Until iter = 20 ' run the test 19 times
tries = 0 ' initialise the counter for random number generation
Cells.Interior.ColorIndex = xlNone ' set all cells to no colour
starttime = Time
nums = 0 ' initialise number of cells that have been chosen
Do Until nums = (ncols * nrows) ' ie do it until every cell in the matrix has been coloured
Randomize
x = Int(Rnd * ncols) + 1 ' generate random number (row) from 1 to 80
Randomize
y = Int(Rnd * nrows) + 1 ' generate random number (column) from 1 to 80
tries = tries + 1 ' increment the counter for random number generation
If Cells(x, y).Interior.ColorIndex = xlNone Then
Cells(x, y).Interior.ColorIndex = 1 'cells(x, y) not previously chosen, so colour it black
nums = nums + 1 ' increment the number of discrete cells chosen
End If
Loop
' once it gets here, all cells have been chosen
endtime = Time
iter = iter + 1
' write results to "Log" sheet
Sheets("Log").Cells(drow, 1) = Format((endtime - starttime) * 100000, "0.0") & "s"
Sheets("Log").Cells(drow, 2) = tries
drow = drow + 1
' do it again until number of tests is 19
Loop
End Sub