# Random Walker Spiral using Probabilities

**URL:** <https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948>\
**Category:** Coding Questions\
**Created:** [November 26, 2018, 11:39am UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948 "2018-11-26T11:39:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 26, 2018, 11:39am UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/1 "2018-11-26T11:39:03Z")

</div>

Hi Everyone,

I am working on an algorithm to create a random walker using probabilities. I want to have a controlled random spiral shape. Like this:

![IMG_2183](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bf41b19bddfd91040d37881132c76136d84548e2.jpeg)

I have create a random walker class first. When i am trying to make it turn, it doesn’t work anymore.

Here is my code:

```auto
Walker[] s = new Walker [1];

void setup(){
  size (400,400);
  background(0);

  smooth();
  
  for (int i = 0; i < s.length; i++){
    s[i] = new Walker (0,0);
  }
  
  frameRate(120);
}

void draw(){
  
  for (int i = 0; i < s.length; i++){
    s[i].display();
    s[i].step();
  }
}

class Walker{
  
  float x,y;
  color c;
  int diam;
  float xdir, ydir;
  
  Walker(float _x, float _y){
    x = _x+5;
    y = _y+5;
    c = 255;
    diam = 5;
  }
  
  void display(){
    translate (width/2, height/2);
    fill(c,10);
    noStroke();
    ellipseMode(CENTER);
    ellipse (x,y,diam,diam);
  }
  
  void step(){
    
    float r = random(1);
    
    //1
    if (x > 0 || x < width/2 && y > 0 || y < height/2){
      
      if (abs(x)>abs(y)){
        if (r < 0.1){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      } else if (abs(y)>abs(x)){
        if(r < 0.1){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      }
    }
 
    //2
    if (x < 0 || x > -width/2 && y > 0 || y < height/2){
      
      if (abs(y)>abs(x)){
        if(r < 0.1){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.7){
          y++;
        } else {
          y--;
        }
      } else if (abs(x)>abs(y)){
        if(r < 0.1){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.5){
          y++;
        } else {
          y--;
        }
      }
    }
    
    //3
    if (x < 0 || x > -width/2 && y < 0 || y > -height/2){
      
      if (abs(x)>abs(y)){
        if (r < 0.3){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.5){
          y++;
        } else {
          y--;
        }
      } else if (abs(y)>abs(x)){
        if (r < 0.5){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.7){
          y++;
        } else {
          y--;
        }
      }
    }
    
    //4
    if (x > 0 || x < width/2 && y < 0 || y > -height/2){
      
      if (abs(y)>abs(x)){
        if (r < 0.5){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      } else if (abs(x)>abs(y)){
        if (r < 0.3){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      }
    }
    
    x = constrain (x,-width/2,width/2);
    y = constrain (y,-height/2, height/2);
  }
}

```

Can someone help me please?

Thank you,

Rémy

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 26, 2018, 12:20pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/2 "2018-11-26T12:20:44Z")

</div>

Here’s my approach…

```auto
float a = 0;
float d = 5;
float x, y, px = 0, py=0;

void setup(){
  size(800,800);
  stroke(255);
  background(0);
}

void draw(){

  translate(400,400);
  
  float r = random(0,0.5);
  x = (r+d)*a * cos(a);
  y = (r+d)*a * sin(a);
  a += 0.05;
  
  line(px, py, x, y);
  px = x;
  py = y;
}

```

I don’t know if that jives with what you want though.

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [November 26, 2018, 12:35pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/3 "2018-11-26T12:35:29Z")

</div>

I haven’t tested the code, but would something like this work?

```auto
float r = 0;
PVector position;
float radius = 1;

void setup() {
  size(600, 600);

  background(255);
  stroke(0);
  
  position = new PVector(width/2, height/2);

  strokeWeight(2);
}

void draw() {
  float ran = random(100); // Pick a random number out of 100
  if(ran > 10) { // If it's more than ten, turn left (90% chance)
    r+= 0.1/radius;
  } else {
    r-= 0.1/radius; // Otherwise turn the other way
  }
  
  position.add(PVector.fromAngle(r)); // Update the position based on the angle

  point(position.x, position.y);
  radius+=0.01; // Increase the radius
}

```

---

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 26, 2018, 1:15pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/4 "2018-11-26T13:15:33Z")

</div>

Hi @Sarah, Hi @TfGuy44,

Thank you for your fast answers. Your proposals are really interesting and i have to say that i thought to use an angle or/and the cos & sin fonctions but wanted to try a more accidental manner using probabilities.

My aim is not to create a spiral with a shaky effect but more a spiral shape from a random behavior (don’t know if i make myself clear here, sorry if it is not the case).

In my drawing, i tried to describe the different parts of the sketch where a walker change his route under the influence of probabilities using the abs() values of x & y.

Does that make sens to you? I might not taking the easiest way to solve that problem, i don’t know…

Thank you,

Rémy

---

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 27, 2018, 12:58pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/5 "2018-11-27T12:58:21Z")

</div>

Any one?

Thank you,

Rémy

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [November 28, 2018, 7:57am UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/6 "2018-11-28T07:57:27Z")

</div>

So from what I understand, you want a walker that has a higher chance of going in one direction that the other, making it move in a spiral motion?

---

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 28, 2018, 12:01pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/7 "2018-11-28T12:01:52Z")

</div>

Hi @Sarah

Yes, exactly. That is why i used the probabilities regarding where the walker is in space.

Thank you,

Rémy

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [November 28, 2018, 12:25pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/8 "2018-11-28T12:25:39Z")

</div>

This is how I would do it:

```auto
PVector position;
PVector velocity = new PVector(1,0);

void setup() {
  size(800, 800);
  stroke(255);
  background(0);
  position = new PVector(width/2, height/2);
  strokeWeight(2);
}

void draw() {
  velocity.add(-velocity.y/10,velocity.x/10); // Apply perpendicular vector
  position.add(velocity); // Update position
  point(position.x, position.y);
}

```

It adds a perpendicular vector to the velocity, which makes it slightly turn.

---

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 28, 2018, 1:06pm UTC](https://discourse.processing.org/t/random-walker-spiral-using-probabilities/5948/9 "2018-11-28T13:06:51Z")

</div>

Really interesting the PVector solution. I will run with that.  
Just being curious, do you know what was wrong in my code?

Thank you @Sarah
