FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Random element from an array?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Random element from an array?  (Read 533 times)
Phiil

WWW
Random element from an array?
« on: Feb 23rd, 2005, 4:48am »

Hi folks,
 
Quite simply, how do you go about extracting a random element from an array?
 
I've set up an array,  
int [] majorscale = {0,2,4,5,7,9,11,12};
and I'd like to be able to have an event which acts using mouseY plus a random value from the array majorscale.
 
My guess for the syntax was:
 
midiOut.setNoteOn(9,mouseY+random(majorscale),mouseX);
 
but I get this error message:
"Semantic Error: No applicable overload for the method named "random" was found in type "BApplet". Perhaps you wanted the overloaded version "float random(float $1);" instead?"
 
I'm putting this here as I'm pretty sure this is likely to be a basic syntactical error....
 
As you might have gathered, I'm playing around with the ProMIDI libraries.
 
Thanks,
 
Phil
 
eskimoblood

222550793222550793 WWW
Re: Random element from an array?
« Reply #1 on: Feb 23rd, 2005, 10:14am »

Try this:
Code:

 int [] majorscale = {0,2,4,5,7,9,11,12};  
 int mj=int(random(majorscale.length));
 midiOut.setNoteOn(9,mouseY+majorscale[mj],mouseX);  

 
 
Phiil

WWW
Re: Random element from an array?
« Reply #2 on: Feb 23rd, 2005, 1:47pm »

Thank you Eskimoblood
 
I'll give that a try when I've finished my current experiment (I've just got the video libraries working - quite fun).
 
Phil
 
 
 
 
Pages: 1 

« Previous topic | Next topic »