Greetings,
I have a situation where I have three characters that must change their graphic (actually loop & frame of the same view) over the course of ten changes. I don't want their graphics to ever be the same (thus I need thirty graphics) and I have a healthy pool of 75 graphics from which to draw. There are a few complicating factors that I'm not going to go into, but basically I want to write a random function that will not recycle values once they have come up. This is pretty easy to do once or twice by re-randomizing if the value equals a variable that stores historical values, but as the percentage of options left available decreases the engine has to do the random calculation exponentially more times to find a fresh value (which is just messy).
If anyone has any ideas or code snippets they'd like to share on this matter it would be much appreciated. Thank you in advance,
BaRoN
One idea, is to first create an array that carries the sprite numbers, say you want 75 sprites in total, just create an array of 76 elements:
int gspr[76];
At first, set the first element to the total amount of available options (75 here), the rest to be the sprite numbers (it would be easy to use a loop if you import those sprites sequentially so to have sprite numbers say #101 to #175):
gspr[0]=75;
int ii=1;
while (ii<76){
gspr[ii]=100+ii;
ii++;
}
Then make the random function like this:
function RandomSprite(){
if (gspr[0]==0) {//if all sprits were used, do whatever you want to here
} else {
int aa=Random(gspr[0]-1);
int temp=gspr[aa+1];
gspr[0]--;//purges the "used" element
ii=aa+1;
while(ii<gspr[0]+1){
gspr[ii]=gspr[ii+1];
ii+1;
}
return temp;
}
}
I never would have thought of shrinking the random value like that..... it's brilliant! I couldn't use your script exactly (due to the complicating factors I alluded to earlier), but your ideas definitely put me on the right track. I used your array idea in its entirety, then bumped all values of variables "behind" the used one up the queue, while sending the used one to the back -something like this (I don't have the code in front of me at the moment):
rank = Random (14 -turn); //there were only 15 graphics per loop and the multiple loops were the complication
cGuy.Frame =reorder[rank];
used =rank;
while (rank>14) { reorder[rank]= reorder[rank +1]; rank ++}
reorder[14] =used;
turn ++;
Thanks a million!
BaRoN
Yes something like that, I was just giving an idea.
Just do whatever modifications to suit your needs. :)