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

typo in introduction

mnopqr opened this issue · comments

In the code after the text "Taking this further, we could use floating point numbers (i.e. decimal numbers) for x and y instead and move according to an arbitrary random value between -1 and 1." the command

float stepx = random(-1, 1);

should be replaced by

float stepx = random(-1, 2);

Similarly for stepy.

Thanks for writing in. Why do you suggest the 2? That would produce random values between -1 and 2 and weight the walker towards moving to the right (and down.) I believe -1 to 1 is correct.

Sorry, my mistake. The reason for my confusion was that, reading Example I.1, it looked as if I could just keep replacing the step() function in the Walker class by the different pieces of code, leaving everything else unchanged. It might be useful to note that at the point where you change from integer steps to floating point steps, you should also change the first two lines of the Walker class int x; int y; into float x; float y;.

Ah, re-opening I should make this more clear in the text, thanks!

Just writing to say this still hasn't been fixed, and left me puzzled for a few minutes.

Ok, here is the revised text. How does this sound?

Taking this further, we could use floating point numbers (i.e. decimal numbers) move according to an arbitrary random value between -1 and 1.

class Walker {
  // x and y are now floats
  float x;
  float y;

Now that x and y are floats, the output of the random() function can be directly applied.

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