# random walk 3D [boundary + clusters] Random Walk 1.4
Inspired by “The Nature of Code” by Daniel Shiffman and it’s python translator Jake Hebbert.
Using C# in Grasshopper for Rhinoceros

[code language=”csharp”]
// Philipp Siedler
// Daniel Shiffman "The Nature of Code"
// Random Walk 1.4
private void RunScript(int seed, int time, ref object A, ref object B, ref object C, ref object D, ref object E)
{
List <Point3d> pList = new List<Point3d>();
List <Point3d> pListEx = new List<Point3d>();

List <int> xList = new List<int>();
List <int> indexList = new List<int>();

Walker w = new Walker();
Random random = new Random(seed);

int counter = 0;

for(int i = 0; i < time; i++){ int rnd = random.Next(0, 7); w.step(rnd); if(w.boundRebounceCount > 0){
}
else{
}
counter++;

if(indexList.Count() == 1){
indexList = counter;
}

if(w.x == 0 && w.y == 0 && w.z == 1){
counter = 0;
}
}

A = pList;
B = xList;
C = counter;
D = indexList;
E = pListEx;

}

public class Walker
{
public double x;
public double y;
public double z;
public int bound;
public int upperBound;
public int rnd;
public int boundRebounceCount;
public int explosionNum;

//Constructor that takes arguments.
public Walker(){
x = 1.0;
y = 1.0;
z = 1.0;
bound = 10;
upperBound = 100;
rnd = 0;
boundRebounceCount = 0;
}

public Point3d pos(){
Point3d posPt = new Point3d(x, y, z);
return posPt;
}

public int step(int rnd){

int choice = rnd;
int explosionNum = 100;

//GROWING UP

if(boundRebounceCount == 0){
if (choice <= 0){
x++;
}
else if(choice <= 1){
x–;
}
else if(choice <= 2){
y++;
}
else if(choice <= 3){ y–; } else if(z > 5 && choice <= 4){
z–;
}

else if(choice <= 5){ z = z + 3; } if (x >= bound || x <= -bound){ boundRebounceCount = explosionNum; } if (y >= bound || y <= -bound){
boundRebounceCount = explosionNum;
}
if (z <= 0 || z >= upperBound){
boundRebounceCount = explosionNum;
}
}

//EXPLODING
double factor = 1;

if(boundRebounceCount > 1){
if (x >= -bound && choice == 0){
x = x – factor;
boundRebounceCount–;
}
else if(x <= bound && choice == 1){ x = x + factor; boundRebounceCount–; } else if(y >= -bound && choice == 2){
y = y – factor;
boundRebounceCount–;
}
else if(y <= bound && choice == 3){
y = y + factor;
boundRebounceCount–;
}
else if(z <= upperBound && choice == 4){ z = z + factor; boundRebounceCount–; } else if(z >= 0 && choice == 5){
if(z > 10){
z = z – factor;
boundRebounceCount–;
}
}
}
if(boundRebounceCount == 1){
x = 0;
y = 0;
z = 1;
boundRebounceCount = 0;
}

return choice;
}
}

[/code]

Output: