We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
}
PS: I did try to search this issue first. The only one I found was discussion/15492 without a clear answer.
Answers
Please see my answer here:
Your
x
andy
variables are bothint
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:This is why you see your
x
andy
variables only decreasing. To fix this, just changex
andy
tofloat
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;
Because of this:
You're always increasing
x
andy
by a random variable between-1
and2
here, so it's going to tend to increase.Never mind.
I need to make it,
float stepx = random(2)-1;
Why wouldn't you just do this:
http://studio.SketchPad.cc/sp/pad/view/ro.91FXxn8BG2fPL/latest
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.
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 between0
and3
. So if you dorandom(3)-1
, you'll get a number between-1
and2
.Similarly,
random(2)
will return a number between0
and2
, sorandom(2)-1
will give you a number between-1
and1
.It's usually easier to just feed in the range you want instead of doing the subtraction.