Random walk with Actionscript
January 7th, 2007 . by polyGeekIt’s amazing to me how something so simple can be so subtlety beautiful. Below is a demonstration of a random walk, or drunkards walk as it is sometimes called. Simply put, each step is a random distance and angle from the previous step.
The code needed to create this is rather simple. It uses nothing more complex than the drawingAPI and an onEnterFrame. Note: there are no checks to make sure that the last step stays on the visible stage. If you want to play around with this you could do things like randomly change the color, use curveTo instead of lineTo, have multiple walkers of varying colors walking around simultaneously, etc. Use your imagination.
A bit of code that you might find useful are the first two lines of code from the drawVector function. This code finds the point to draw to when it only knows the starting point, distance and angle. This bit could come in handy in a number of instances.











