## Random Walk – Example 10.1

This is an example of a structure that grows through a simple process known as a “Random Walk.” These random walks have several applications in scientific modeling, for example ecosystems, and even financial markets. A random walk also approximates (but not exactly) a scientific phenomenon known as Brownian Motion, which uses much more complex mathematics, but with *sometimes* similar results.

Here we are going to use a random walk to draw a series of 2 different kinds of circles, yellow and blue, which can be the same or different sizes. The rhythm between yellow circles and blue circles can also be changed to create different results.

**Basic Process**

The basic process is incredibly simple. We start with a point, which then gets put into a loop into Data Stream 0. In each of the iterations, the point first moves in one of 3 “x” directions (-1, 0, +1) and afterwards in one of 3 “y” directions (-1, 0, +1)

So the point which in this case is starting in position 5, could end up after each round in any of the nine positions show above. It will stay stationary if both the x and y movement values are equal to 0.

This new point is then contributed to an ongoing tally of points, which is kept in Data Stream 1. The point in its new location is then looped back in Data Stream 0 to undergo the process again and again. In the end there will be a cloud of points. Some spaces may have 2 or more points since the random walk will go back over itself.

The two images above show a polyline connecting the cloud of points in the order that they were drawn, as well as an interpolated NURBS to show how the walk moves back on itself. In both cases 500 points are drawn.

In the script image shown above, an important consideration is how the random numbers are generated and structured within the loop. Also, the Random seed *cannot* be the same for X and Y movement otherwise you will have movement in a straight line, since movement of -1 “x” will always be coupled with -1 “y” since the random number lists will be identical. In this case, the random number list is generated independent of the loop, with random values equal to the total number of repetitions we anticipate. Then, in each round, I use the “List Item” component to call one of the random values equal to the current round, so item 0 in round 0, item 1 in round 1, etc…

**Post Production**

After the process finishes looping, the data is output from the loop. In these examples, the points are output and circles are drawn at each center. To make the pattern a bit more interesting, I dispatched the points into two lists based on a simple True/False test. This is similar to “Cull Nth”, but since I want to keep the items, and not cull them, i made a series of “0” values (which can be used in lieu of “False” in a T/F test and inserted a “1” at the end of the series to make a “True” value. This may not make much sense at first, and maybe there is a better way, but it didn’t occur to me at the time. So the circles that are “False” go on to become yellow circles while the “True” circles become blue. Since most positions are occupied by multiple points, sometimes a blue and yellow circle will be drawn at the same location. I can move the blue circles slightly up in the “Z” direction to have them display before the yellow, or vice versa.

In the first three iterations, I changed only the random number seed.

In the second three iterations, I kept the seed the same but changed the rhythm of Blue circles.

In the third set of 3 iterations, I changed the size of the various circles, as well as which set of circles displays on top.

And finally, I though I would add this image of a quick test I did where instead of blue circles, the script outputs trees in a certain rhythm. The tree size is based on how old they are. While it is a silly design, it actually follows to some degree ecological principles where a plant has random seed dispersal. OK, i just made that up 🙂

Hi, thank you for this post! I’m new to grasshopper, so please forgive the naivety of my question. How would I create a “random walk” within a particular boundary? For instance, say I wanted to show the randomization of variables within a county outline…how would I assign a boundary to the randomized points? I hope that makes sense…

Thanks,

Clay

LikeLike

Hi Clay. This is certainly doable. You would need to do a second test. After the point is moved in a random direction, you would test it to see if it is still within a boundary using a “Point in Curve” command. If this comes back as “false” you would need to re-roll the dice, so to speak. The coding might be a bit difficult as you would need to do a “loop within a loop.” If I think of a better solution I will let you know.

LikeLike

Thank you for your reply. I think I understand the basic concept you’re referring to. I’m just having trouble getting the random points to respond to the curve boundary I have created because it’s run through so many other components. Like you said, it’s a “loop within a loop” and it’s tricky for me to figure out. I’ll keep working with it. Thanks!

LikeLike

Hi! Thanks for this post! Could you please clarify where you are getting the “loop” component from. I can’t seem to find one that works the same.

LikeLike

You need to get the “Anemone” plugin for free from Food for Rhino to do loops.

LikeLike