Stupid stupid question but here we are..
so, I want to generate some random events for an iPhone game but I guess this could be applied to any game written with a framework that suppports calls to "C" functions as I am using CCRANDOM that calls the random(void) function from the C stdlib.h.
I have written a function "verifyEvents" that I call each time I load a particular view (game scene).
To associate a probability to an event I assigned a value to each event (e.g. kill player: 0.05, give extra bonus 0.08).
In the function I generate a random number using CCRANDOM_0_1() and if the number is less than the assigned value I trigger the event (below there is the code).
Is this the best approach or do you use something else in your games?
-(void) verifyEvents
{
float value = CCRANDOM_0_1() ;
float eventPValue = 0.05f;
if(value<eventPValue){
CCLOG(@"EVENT!");
}
}
PS: CCRANDOM_0_1() is defined as following:
#define CCRANDOM_0_1() ((random() / (float)0x7fffffff ))
rand()instdlib.hreturns a value between 0 andRAND_MAX, which is defined as 32768, not0x7fffffff(which is 2147483647) – bobobobo Jul 29 '12 at 18:34