random(-1,1) Processing Bug? Or, coding nuance?

I'm getting an unbalanced random distribution (heavily negative) from

float stepu = random(-1, 1); //red

And a balanced random distribution from

float stepx = random(3)-1; //blue

Shouldn't these two lines be roughly equivalent in results? Red is clearly weighted negative in the comparison code.

Is this a Processing bug? If not, can someone clarify the difference. Or, what I'm doing that's causing slanted results. Thanks.

int x,y,u,v; 
void setup(){
  size(200,200);
  stroke(0);
  x = width/2;
  y = height/2;
  u = x;
  v = y;
}
void draw(){
  float stepu = random(-1, 1);
  float stepv = random(-1, 1);
  u += stepu;
  v += stepv;
  stroke(255,0,0);
  point(u,v);

  float stepx = random(3)-1;
  float stepy = random(3)-1;
  x += stepx;
  y += stepy;
  stroke(0,0,255);
  point(x,y);
}
  • Processing v3.2.1
  • Windows 10 64b

PS: I did try to search this issue first. The only one I found was discussion/15492 without a clear answer.

Answers

  • edited September 2016 Answer ✓

    Please see my answer here:

    Your x and y variables are both int types. That means that they don't have a decimal part, so any time you add or subtract from them, they are truncated. Here are some examples:

        int x = 1;
        x = x + .5;
        //1.5 is truncated, and x stays 1
    
        int x = 1;
        x = x - .5;
        //.5 is truncated, and x becomes 0
    

    This is why you see your x and y variables only decreasing. To fix this, just change x and y to float types, so they keep track of the decimals.

  • my gosh! thank you. doh!

  • But wait, then why does the situation switch when I assign my variables as floats? Now the blue is slanted to the positive.

    Changed line 1 to:

    float x,y,u,v;

  • edited September 2016 Answer ✓

    Because of this:

    float stepx = random(3)-1;
    float stepy = random(3)-1;
    

    You're always increasing x and y by a random variable between -1 and 2 here, so it's going to tend to increase.

  • Never mind.

    I need to make it,

    float stepx = random(2)-1;

  • edited September 2016

    float stepx = random(2)-1;

    Why wouldn't you just do this:

    float stepx = random(-1, 1);
    
  • edited September 2016

    That's preferred and what I did initially under the mismatched data types. Then I noticed that random(3)-1 appeared to perform as anticipated, leading me to look the wrong way to solve the problem.

    Thanks for the quick help.

  • Then I noticed that random(3)-1 appeared to perform as anticipated

    Just think about what the random(x) part will return, then do the subtraction from both sides.

    For example, random(3) will return a number between 0 and 3. So if you do random(3)-1, you'll get a number between -1 and 2.

    Similarly, random(2) will return a number between 0 and 2, so random(2)-1 will give you a number between -1 and 1.

    It's usually easier to just feed in the range you want instead of doing the subtraction.

Sign In or Register to comment.