Out of boredom, I set out to try and make a random Excel maze generator. To start I had it plot the progression on the spreadsheet by step count just to sanity check my process. If it draws itself into a dead end the process restarts. What i have found is that it will try twice (boxing itself in) and then never get any farther. But what is most interesting is every time I run the macro it is the same two patterns generated. If I fiddle with the code a bit it will draw something different, but again will repeat the same two patterns. This leads me to think the RND function is not truly random. Does anyone have insight on it, and/or what can be done to truly randomize? I have included the code, which I know needs some cleaning up and has one previous RND line I was using.
Code:
Sub drawmaze()
linebeg: 'start
Range(Cells(1, 1), Cells(30, 14)).ClearContents
w = 0
Dim p(31, 15) As String
For s = 1 To 30
p(s, 15) = "X"
p(s, 0) = "X"
Next s
For t = 1 To 15
p(31, t) = "X"
p(0, t) = "X"
Next t
p(1, 1) = "X"
rw = 1
cl = 1
Cells(rw, cl) = "X"
line2: 'start
u = 0: d = 0: l = 0: r = 0
If rw = 1 Then u = 1: l = 1
If rw = 30 Then d = 1: l = 1
If cl = 1 Then l = 1: u = 1
If cl = 14 Then r = 1: u = 1
If p(rw - 1, cl) = "X" Then u = 1
If p(rw + 1, cl) = "X" Then d = 1: MsgBox "D"
If p(rw, cl - 1) = "X" Then l = 1
If p(rw, cl + 1) = "X" Then r = 1
If u = 1 And d = 1 And l = 1 And r = 1 Then GoTo linebeg
line1: 'find allowed move
q = 0
'Z = Round(Rnd() * 4, 0)
Z = Int((3 - 0 + 1) * Rnd + 0)
If Z = 0 And u = 0 Then p(rw - 1, cl) = "X": q = 1: rw = rw - 1
If Z = 1 And d = 0 Then p(rw + 1, cl) = "X": q = 1: rw = rw + 1
If Z = 2 And l = 0 Then p(rw, cl - 1) = "X": q = 1: cl = cl - 1
If Z = 3 And r = 0 Then p(rw, cl + 1) = "X": q = 1: cl = cl + 1
If q = 0 Then GoTo line1
w = w + 1
Cells(rw, cl) = w
If rw = 30 And cl = 14 Then GoTo lineend
GoTo line2
lineend: 'completed
End Sub