Stupid stupid question but here we are..
so, I want to generate some random events in an iOS application (in my case a game). 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(), an iOS macro based on the random() function defined in stdlib.h. If the number is less than the assigned value I trigger the event (below there is the code).
#define CCRANDOM_0_1() ((random() / (float)0x7fffffff ))
Is this the best approach or do you use something else in your apps?
-(void) verifyEvents
{
float value = CCRANDOM_0_1() ;
float eventPValue = 0.05f;
if(value<eventPValue){
CCLOG(@"EVENT!");
}
}
Ok, following the suggestion of @lukya I asked the question on gamedev and did some more research. By far I got two answer which seem quiet interesting (which should be read together with the comments on my question).
EDIT: Forgot to put the link here as well (was only in the comment):
https://gamedev.stackexchange.com/questions/33236/simulating-probability