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 › Noob: simpler way to generate random integers
Page Index Toggle Pages: 1
Noob: simpler way to generate random integers (Read 562 times)
Noob: simpler way to generate random integers
Aug 20th, 2009, 10:05am
 
Hi. I'm quite new to processing and I've been working a lot with random variables. The problem though is I only know one way to make them, and that is by generating a random float and then using int() to convert them. I haven't been able to find another way to do this, but surely there must be a more elegant way?
Re: Noob: simpler way to generate random integers
Reply #1 - Aug 20th, 2009, 11:04am
 
Code:
int r = int(random(99)); 



Isn't it elegant enough?
Re: Noob: simpler way to generate random integers
Reply #2 - Aug 20th, 2009, 11:26am
 
Or you can cast the returned value as an int:
Code:
int r = (int) random(99); 



That's perhaps slightly nicer as it avoids putting two closing brackets together...  Not sure which is most efficient/quickest though.

Otherwise there are probably libraries that will do this, but I wouldn't bother with those unless they also provide additional functionality that you need...
Page Index Toggle Pages: 1