Random Walk 1.2
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.2
private void RunScript(int seed, int time, ref object A)
{
List <Point3d> pList = new List<Point3d>();
Walker w = new Walker();
Random random = new Random(seed);
for(int i = 0; i < time; i++){
int rnd = random.Next(0, 6);
w.step(rnd);
pList.Add(w.pos());
}
A = pList;
}
public class Walker
{
public int x;
public int y;
public int z;
public int rnd;
public Walker(){
x = 0;
y = 0;
z = 0;
rnd = 0;
}
public Point3d pos(){
Point3d posPt = new Point3d(x, y, z);
return posPt;
}
public int step(int rnd){
int choice = rnd;
if (choice == 0){
x++;
}
else if( choice == 1){
x–;
}
else if(choice == 2){
y++;
}
else if(choice == 3){
y–;
}
else if(choice == 4){
z++;
}
else if(choice == 5){
z–;
}
return choice;
}
}
[/code]
Output:
[youtube https://www.youtube.com/watch?v=ASwpuJ9oZJQ?rel=0&controls=0&showinfo=0&w=560&h=315]