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.
IndexProgramming Questions & HelpSyntax Questions › random() question
Page Index Toggle Pages: 1
random() question (Read 391 times)
random() question
Apr 11th, 2008, 1:22am
 
Hi, how would I go about producing random numbers between ..say..  -10  to -5  AND 5 to 10 - rather than between -10 and 10.  The program sketch I am working on is a shape that bounces off walls at random speeds, and I'd like to remove the slower crawls if possible.
J
Re: random() question
Reply #1 - Apr 11th, 2008, 2:46am
 
two calls to random() should do it...

1) by flipping the sign half the time:

float n = random(5,10);
if (random(1)<0.5) n = -n;

2) or flip by multiplying by random -1 or 1:

float n = random(5,10) * (floor(random(2))*2-1);
Re: random() question
Reply #2 - Apr 12th, 2008, 12:19am
 
Thanks
Page Index Toggle Pages: 1