Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A old Software Drunkards Walk
A old Software Drunkards Walk
#1
Back in the 80's I run across a little program written in BASIC that was called Drunkard's Walk. The program used BASIC's random command to randomly move a small ball or dot  around the screen, it was supposed to simulate the Brownian motion of a single molecule or atom.

I rewrote the program and expanded it for my Commodore 64 and "peeked" and "poked" the poor machine  for days at a time simulating a few dozen particles.

I eventually rewrote half of the program in 6502 machine code and tried simulating hundreds of small randomly moving dots bouncing off of each other and the 4 sides. It  was never fast, but in trying to speed the program up and play with the test to simulate a charging battery I hit on a very simple version that produced surprisingly nice Frost like patterns along the top or bottom of the screen.

The program below is a version written for a free version of BASIC called "Just BASIC", this version of BASIC isn't well suited to the program and a large number of the lines of code are there just to make the object list graphics focused language handle a program that would be almost  trivial to write in a version of BASIC that is oriented toward screen Pixel manipulation commands.

The graphics aren't quite right, if you ever remove the working screen from the top it fails to refresh portions of the image and it still has a now hopefully rare crash with a object list stack overflow requiring a CONTROL-ALT_DELETE  to end version 1 of Just BASIC.

Below the double line is a copy pasted Just BASIC version of a Drunkards Walk, hopefully just copying and pasting will let you copy and run it in your own copy of Just BASIC.

Have fun growing frost with a large number of sequential drunkards walking.
===============================================================================================

000 REM A 2 dimensional graphical version of a drunkards walk depending on the Random number generator it should eventually form a frost like pattern.
    REM I had a variation of this program running on a old Commodore 64 several years ago, it produced "frost on glass" type patterns after a long run.
    REM The C64 took 8 to 48 hours of Drunks piling up depending on the resolutions and setting I used to get a pattern.

    REM Most modern BASIC can't read the screen so you need to set up a program accessable shadow of the drankards movement on screen
    REM I picked a String function for compatability, it would be faster to use a single byte interger array, but that is often version dependent.
010 DIM scrn$(512,512) :REM since many modern versions of BASIC can't easily check window/screen pixel states BASIC needs a way to remember where the other "D's" are "stuck".

    REM next few lines initialize the number of drunks that take a walk and where they stagger out on the screen.

015 PRINT"How many drunks do you want to walk? ": PRINT "There is no error checking on the number entered, I hope you know how to use Control Alt Delete."

017 PRINT"At least 1,000 Drunks for better image build up, but more takes longer so at fist go with 100 or 200."

020 INPUT "Enter Number of Drunks. "; Drunks

023 PRINT "The Bar is hidden below the middle of the screen so your drunks currently appear in the middle."
024 PRINT "Then then wonder toward the top of the window where they stick and build up a pattern."
025 PRINT "IF you want to change this you will have to dig into program code and move the bar."
027 startx = 256 : starty = 500 :REM Roughly the center of the screen for longer/larger pattern increase the Y value, just stay below 500.

030 IF startx > 500 OR startx < 10 OR starty > 500 OR starty < 10 THEN startx = 255 : starty = 255
    REM Error check, No I don't trust the user this is an attempt to keep the bar on the screen, I don't won't even more "D"runks wandering the multiverse, I've lost 1000's

    REM fill shadow screen with N and D's to represent "N"o Drunk and "D"runks, Put a wall near the borders,"Don't won't our drunks wondering off into screen cyberspace do we.
040 FOR xf = 1 to 512
043     FOR yf = 1 to 512 : Rem there are faster ways, but those can be version dependent.

            REM These two lines creates  box of D's and open space for our Stagger (We don't want completely Lost drunks, now do we.)

045         IF xf = 9 or xf = 501 THEN scrn$(xf,yf)="D" else scrn$(xf,yf)="N"

047         IF yf = 9 or yf = 501 THEN scrn$(xf,yf)="D" else scrn$(xf,yf)="N"

050     NEXT yf :REM depending on how the Dim command creates arrays may have to use 0 to 511 instead of 1 to 512.
053 NEXT xf :REM This goes for Line 010 as well the 512 may need to be replaced with a 511 depending on version of BASIC.

    REM The next commands Set up a 512 by 512 window for displaying the Drunkards Walk as a staggering dot. (Wishful thinking in Just BASIC)
    WindowWidth = 512
    WindowHeight = 512

095 open "Drawing" for graphics as #c
    PRINT #c, "down"         :REM put the pen down most, most modern basic have moved away from a pixel focus to graphical objects.
    PRINT #c, "fill black"   :REM most of the time this makes graphics easy, sadly not for this version of a pixel focuse Drunkards walk simulator that is far from true.
    PRINT #c, "flush"        :REM Jut BASIC already has a few problems with displaying and refreshing graphics in a window, the pixel focus can lead to stack overflow.
                              REM CONTROL-ALT-DELETE is usually a good way to clear off your old crashed programs, you did remember to save a copy befor running?

100 FOR stagger = 1 to Drunks

105 xold = 1 : yold = 1   :REM this is to address a logic flaw for a new drunk's previously off screen position so scr$(1,1) needs to always have a "N"

110     x = startx: y = starty :REM New drunk has eppeared from Bar Location to start his Drunkards Walk.
        REM Warning an individual Drunk can potentially stagger for a long time.
        REM Label or line 125 give the drunkard a sharp TILT or GRAVITY in this case and let your drunks quickly stagger down hill.

115     scrn$(startx,starty)="D":REM The "D"runk is put back in the shadow screen at the start or bar's location.
        PRINT #c, "color blue"
        PRINT #c, "Set ";x;" ";y  :REM The "D"ot on the screen at the bar's location.

120     xrnd = INT(RND(1)*2) : yrnd = INT(RND(1)*2):REM get a random integer either 0 or 1
        REM command "INT(RND(1)*2)" randomly reduces the random fractionnumber between 0 and 1 to either 0 or 1
        REM Different versions of BASIC will need different math and test to change the random numbers into the random -1 or 1.

125     yrnd = yrnd - 1 :REM put a direction
        REM leaving 125 remarked and entering say 50,000 for the drunks should SLOWLY show how random your Random command is
        REM If your device has accelerometers and if you have RFO BASIC it is possible to read and use the device's tilt to "guide" your "D"runk's Walk.

130     IF yrnd = 1 THEN ystep = 1 ELSE ystep = -1
        IF xrnd = 1 THEN xstep = 1 ELSE xstep = -1
        IF x > 509 or x < 3 or y >509 or y < 3 THEN GOTO 200
        REM keep the "D" positioned well within the shadow window array (512 by 512)

135     IF scrn$(x + xstep , y + ystep) = "D" THEN GOTO 200
        REM The current "D"'s next step will hit something and stopped, so leave the Dot and "D"runk "Stuck" in his current position and let the next "D"runk stagger out.

140     scrn$(xold,yold)="N": REM replace the "D"runk at the old position with N.
145     scrn$(x + xstep,y + ystep)="D" :REM put the "D"runk at the new stagger position

149     PRINT #c, "color black"
150     PRINT #c, "SET "; xold;" "; yold
        PRINT #c, "discard"
        REM Black out the "D"runk's previous position

154     PRINT #c, "color blue"
155     PRINT #c, "SET "; (x + xstep); " "; (y + ystep)
        REM PRINT #c, "flush"
        PRINT #c, "discard"
        REM These commands will vary GREATLY depending on version of BASIC:To get Drankards Walk to work in "Just BASIC" has nearly double the program's size.


170     xold = x: yold = y: REM remember where the druck has just stepped from, There can be a logic flaw for the drunk just leaving the bar I use scrn$(1,1)="N" as the offscreen old.

180     x = x + xstep : y = y + ystep : REM finally update the current drunks position, this is also important label 170 and 180's placement are important.

190     GOTO 120 :REM Keep the drunk staggering randomly until he hits another "D". This is a dangerously open ended goto loop.

200 PRINT "Drunk "; stagger;" Just hit something and stopped."
201 next stagger
    REM really need to add a way to save the screen or at least save the Shadow screen array.

205 Input "Press enter to close graphics screen"; xold
    Print" The program has ENDED!"

210 CLOSE #c

250 END
    REM just ends the program with no provision for holding the final screen of how your drunks stack up.
    REM The graphics you mostly saw on the graphics screen are stored in scrn$ array until the program ends.

=====================================================================
Don't copy the above double line.

The REMarks should be removable with no problem.

Hope you enjoy this software version of a "Drunkards Walk" and the frost like patterns it can produce.

HMelton
Reply
RE: A old Software Drunkards Walk
#2
Thanks! I used to write little 1-dimensional drunkard's walk programs (or mule and haystacks, if you know that metaphor) when I got bored during my first couple jobs in NY, but never a 2-D version, and I never even imagined trying to do a graphical version.
-- Bob

I have been Roland, Beowulf, Achilles, Gilgamesh, Clark Kent, Mary Sue, DJ Croft, Skysaber.  I have been 
called a hundred names and will be called a thousand more before the sun grows dim and cold....
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)