nature-of-code / noc-book

The Nature of Code book (archived repo, see README for new repo / build system!)

Home Page:http://natureofcode.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feedback from reader

shiffman opened this issue · comments

Hello Daniel,

There is a segment of code excerpted from the Introduction.

void step() {
                    //Yields any floating point number between -1.0 and 1.0
    float stepx = random(-1, 1);
    float stepy = random(-1, 1);
    x += stepx;
    y += stepy;
  }

I think it would causes some misunderstanding. According to the text, it would be easier to conclude that this segment of code has the same effect as this one :

  void step() {
    int stepx = int(random(3))-1;
    int stepy = int(random(3))-1;
    x += stepx;
    y += stepy;
  }

But the program shows that the walker using that code will go up and left, which may not be anticipated by some people.

At first, I was confused, because the probability of taking any direction is not equal. After I read the text several times, I found, actually the text does not state that segment of code would produce the same result. In fact, there is no problem of it. But, if I did not try to run the code, I would think they are no different. And maybe some others would think the same way.

So, instead of using random(-1, 1), I think it would be better to use random(-1, 2) here.

I have tested the code several times and am not finding a significant enough bias that merits a change from random(-1,1) though I do see the issue.