# Nature of Code book (random walker)

**URL:** https://discourse.processing.org/t/nature-of-code-book-random-walker/28637
**Category:** Beginners
**Created:** [March 18, 2021, 12:38pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637 "2021-03-18T12:38:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ariadne](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@Ariadne](https://discourse.processing.org/u/Ariadne)
#### Post date: [March 18, 2021, 12:38pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/1 "2021-03-18T12:38:05Z")

</div>

I have been trying to follow with the Nature Of Code processing book **in p5js**.  
The following code does not work:

```auto
class Walker {
  contructor() {
    this.x = 250;
    this.y = 250;
  }
  
  display() {
    stroke(255);
    point(this.x, this.y);
  }
  
  step() {
    const random_choice = random(4);
    
    switch (random_choice) {
      case 0: {
        this.x += 1;
        break;
      }
      case 1: {
        this.x -= 1;
        break;
      }
      case 2: {
        this.y += 1;
        break;
      }
      case 3: {
        this.y -= 1;
        break;
      }
    }
  }
}

let w;

function setup() {
  createCanvas(500, 500);
  w = new Walker(width, height);
}

function draw() {
  //background(0);
  
  w.display();
  w.step();
  
}

```

> Sorry for the small description! I am currently in ahurry.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 18, 2021, 1:30pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/2 "2021-03-18T13:30:34Z")

</div>

this works, but is not good code

```auto
let x = 250;
let y = 250;

class Walker {

  contructor() {
    // x = 250;
    /// y = 250;
  }

  display() {
    stroke(0);
    fill(0);
    circle(x, y, 2);
  }

  step() {
    let random_choice = int(random(4));

    switch (random_choice) {
      case 0: {
        x += 1;
        break;
      }
      case 1: {
        x -= 1;
        break;
      }
      case 2: {
        y += 1;
        break;
      }
      case 3: {
        y -= 1;
        break;
      }
    }
  }
}

let w;

function setup() {
  createCanvas(500, 500);
  w = new Walker(width, height);
}

function draw() {
  //background(0);

  w.display();
  w.step();
}
//

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 18, 2021, 1:36pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/3 "2021-03-18T13:36:47Z")

</div>

> [@Ariadne](#):
>
> ` contructor() {`

You’ve mispelled **constructor()**: 😉  
` constructor() {`

> **[constructor - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor)**
>
> The constructor method is a special method of a class for creating and initializing an object instance of that class.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 18, 2021, 1:52pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/4 "2021-03-18T13:52:02Z")

</div>

Yes. I feel stupid now… 😉

I also changed this: ` let random_choice = int(random(4));`

```auto
class Walker {
  
  constructor() {
    this.x = 250;
    this.y = 250;
  }
  
  display() {
    stroke(0);
    point(this.x, this.y);
   // circle(this.x, this.y,4);
  }
  
  step() {
    let random_choice = int(random(4));
    
    switch (random_choice) {
      case 0: {
        this.x += 1;
        break;
      }
      case 1: {
        this.x -= 1;
        break;
      }
      case 2: {
        this.y += 1;
        break;
      }
      case 3: {
        this.y -= 1;
        break;
      }
    }
  }
}

let w;

function setup() {
  createCanvas(500, 500);
  w = new Walker(width, height);
}

function draw() {
  //background(0);
  
  w.display();
  w.step();
  
}
//

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 18, 2021, 1:57pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/5 "2021-03-18T13:57:18Z")

</div>

> [@Chrisir](#):
>
> I also changed this: ` let random_choice = int(random(4));`

- JS offers a variety of keywords to declare variables w/.
- My fav is [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) b/c most of the time variables don’t need to be re-assigned.
- B/c the variable _random\_choice_ is only accessed within _switch_’s parens we can simply eliminate the variable to shorten the code further:

```auto
  step() {
    // let random_choice = int(random(4)); // no need

    switch (~~random(4)) {

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 18, 2021, 1:58pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/6 "2021-03-18T13:58:41Z")

</div>

and I added `int()`

Chrisir

---

<div class="post-metadata">

### Author: ![Ariadne](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@Ariadne](https://discourse.processing.org/u/Ariadne)
#### Post date: [March 18, 2021, 3:06pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/7 "2021-03-18T15:06:49Z")

</div>

Thank you! By the way, in your reply to @Chrisir, you code thefollowing:

```auto
  step() {
    // let random_choice = int(random(4)); // no need

    switch (~~random(4)) {

```

can you say what this does?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 18, 2021, 3:13pm UTC](https://discourse.processing.org/t/nature-of-code-book-random-walker/28637/8 "2021-03-18T15:13:48Z")

</div>

Using the unary bitwise operator [`~`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT) twice truncates a value by removing its fractional part.

It’s a shorter & more performant replacement for **int()**: 😛  
[https://p5js.org/reference/#/p5/int](https://p5js.org/reference/#/p5/int)
