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.
IndexDiscussionExhibition › urn
Page Index Toggle Pages: 1
urn (Read 338 times)
urn
Jan 31st, 2009, 7:15pm
 
Hi, here I share you a TINY class that simulate a urn object. I took (stole... as you wish lol) the idea from max/msp.

Urn is a random without duplication, thats it.

Here is the code, hope it'll be usefull for someone lol

------------------------------

Urn __urn;

void setup(){
 __urn = new Urn(10);
}

void draw(){  
 int f = __urn.update();    
 if(f == -1) noLoop();//__urn.reset();  
 println(f); //print element took from urn  
}

class Urn{

 int maximum; //max array size
 int[] urn;
 int index; //current level

 Urn(int _maximum){
   maximum = _maximum;
   urn = new int[_maximum];
   for(int i = 0; i < maximum; i++) urn[i] = i;      
   reset();
 }

 void reset(){    
   index = maximum - 1;
 }

 int update(){
   if(index < 0) return -1;
   int randomIndex = floor(random(index));
   int returnValue = urn[randomIndex];
   urn[randomIndex] = urn[index];      
   urn[index] = returnValue;
   index--;
   return returnValue;
 }

}

Page Index Toggle Pages: 1