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 › best choose for randomize
Page Index Toggle Pages: 1
best choose for randomize (Read 578 times)
best choose for randomize
Feb 17th, 2010, 9:13am
 
hi guys, tomorrow at 9 o clock i will discourse my university degree here in Milan. I am working on a robot with arduino, processing and roborealm.
I have to randomize a value to choose a quotation in an array, or something like:
int randomizer = random(0, 10);
if (randomizer == 1) { ttspeak(HI!); }
if (randomizer == 2) { ttspeak(HOLA!); }

i use ttspeak library to sintetize speech. I recall this quotation from arduino and processing with if statements based on sensor and camera input.
Re: best choose for randomize
Reply #1 - Feb 17th, 2010, 9:52am
 
int randomizer = random(0, 10);

random() returns a float so you'll need to cast it

int rnd = (int)random(0, 10);

i'd also use a switch() rather than if then else else else else else...

but an array of phrases from 0 to 9 is probably better:

ttspeak(phrase((int)random(0, 10)));

(edits, edits, sorry)
Re: best choose for randomize
Reply #2 - Feb 17th, 2010, 10:22am
 
Does this help?

Quote:
String[] msgs =
{
  "Hi!",
  "Hola",
  "Hello",
  "Hallo",
  "G'day"
};

void draw()
{
  int msgIndex = (int)random(msgs.length);
  String msg = msgs[msgIndex];
  
  println( msg );
}

Re: best choose for randomize
Reply #3 - Feb 18th, 2010, 11:27am
 
Oh my god this script is awesome!!
Thank you very much!!
Page Index Toggle Pages: 1