# Creating A Random Walk pendant In OPen Scad In the procedural world the random walk is probably the easiest algorithm. Basically start in a given position roll a dice and move in any direction. We are going to use this to create a stylish looking pendant to keep this simple we will only use the cardinal points. We will be using openscad and at the end we will add a loop so we can place it in a chain.

In order to do this we need to understand what is a recursive function and how do they work. Recursion is when a function can be decomposed into itself. For example the easiest recursive function that I can think of is counting.   The Count(5) = Count(4) +1 if we take this is into a form using a variable count( a ) =  count( a – 1 ) + 1 then we have a base case that on 0 we return 0 in openscad that looks like this:

 function count(a) = a == 0 ? 0: count(a-1);//we can call that function like count(5);

Now that we are pro’s at recursion. Lets take a look an see what else we need to understand to get this done. Well the next step is to understand how to do randoms in openscad. Is pretty simple you use the function rands the first para is the lower bound, the second one the top bound and the third one the amount that we need.

 //amount of steps in our functioncantidad = 100;direction = rands(0,4,cantidad);

Cool so if we look at our algorithm we just know need to basically create a recursive function to tell us if we should draw or not for each step based on the number of the step.

They look like this for my example:

 function calcx(vector , pos) = pos == -1?0:vector[pos]>3?calcx(vector,pos-1)+1:vector>2?calcx(vector,pos-1)-1:calcx(vector,pos-1); function calcy(vector, pos) = pos == -1?0:    vector[pos] <1? calcy(vector,pos-1)+1:vector[pos] <2?calcy(vector,pos-1)-1:calcy(vector,pos-1);

Then we just need to call each step so it gets drawn the code is just a loop.

 for  (i = [0:cantidad-1]){     translate([calcx(direction,i),calcy(direction,i),0])cube(1);    }

To add the loop to add the chain all we do is add 4 blocks and make a hole in it using difference at the start. You can get the entire code from my thingiverse page. http://www.thingiverse.com/thing:1231928

You can also order some of them from my Shapeways shop
http://www.shapeways.com/shops/alfredo

If you have any questions or something is not clear please let me know I love answering questions and trying to make the articles better.