We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
random() as int (Read 1419 times)
random() as int
Oct 10th, 2007, 8:05pm
 
This is just something that bites me every once in a while (even though I know better!) and I just thought I'd mention it.

Consider needing random integer values of -1,0,1 (say you need movement deltas for a brownian motion -type walker or something, but any inclusive range of integer values will have the same issue, this range just used for example)

I realize that random(-1f,1f) implies "-1f+random(2f)" and returns [-1,1) so that if you did "(int)(random(-1,1))" you'd get only the values -1,0.

And the correct way to write it would be:
(int)(random(-1f,2f))
-1+(int)(random(3f))
(int)(-1f+random(3f))
(int)(random(3f)-1f)
etc

Problem is, none of those are likely to be intuitive for a beginner.  So just makes me wonder if there might be an opportunity for some other variant of the random() function that would help clarify the issue?
Re: random() as int
Reply #1 - Oct 10th, 2007, 8:34pm
 
i think what you're getting is actually more of an issue with how ints are floored in a cast (particularly around 0). to produce 3 random integer values -1, 0, and 1, you should use:

int r = int(random(3)) - 1;

not the version that produces a range. if anything, we should perhaps remove the ranged version, to prevent people from falling into this case.
Re: random() as int
Reply #2 - Oct 11th, 2007, 11:34pm
 
...especially because if you actually try to use (int)(random(-1,1)) the only value that ever comes out is 0:

Code:

for (int i=0; i<100; i++){
println( (int)(random(-1,1)) );
}


(int) always rounds towards zero, which is a tricky gotcha - there was a bug in the core a while back because (int) was being used to round floating point values for pixel-based clipping, and it was very subtle because it only worked incorrectly when the floating point value was between -1 and 0.

You could either use Ben's suggestion, or floor(random(-1,2)) to get what you want.  I like floor because it explicitly acknowledges what you're trying to do.

I don't know that I agree about removing the ranged version, I find it very useful whenever I need a random floating point value.  What I might agree with is decoupling the Processing random() function from the Java implementation, which in my opinion is crummy and slow, like most built in random functions in programming languages...(actually, I feel this way about a lot of the Math.* functions, for that matter - I encourage you to actually try writing the obvious versions of simple math functions and comparing the performance versus the Math.* versions, you might be surprised!  Some of this is because Math.* uses doubles instead of floats, so you end up with a cast in and a cast out, but I suspect some of it is that Java's core developers have never taken mathematical speed to be very important, always preferring a slow, correct, and simple math library to a fast or complete one).
Re: random() as int
Reply #3 - Oct 12th, 2007, 7:04pm
 
Thanks both.  This isn't a problem for me per se, I do understand the problem and how to correct for it.  (It only bites me when I carelessly forget to import my own lib with a "int random(int,int)" that behaves the way I expect it to)

 It's not a P5 bug, and I know int() != floor(), but thought it might be worth pointing out since it seems like a fairly common usage and a bit more prone to error than the rest of core.  I don't know that there's necessarily anything to be done about it, just food for thought.  Smiley
Page Index Toggle Pages: 1