Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;


PS: CCRANDOM_0_1() is defined as following:

#define CCRANDOM_0_1() ((random() / (float)0x7fffffff ))
share|improve this question
Your random function is wrong. rand() in stdlib.h returns a value between 0 and RAND_MAX, which is defined as 32768, not 0x7fffffff (which is 2147483647) – bobobobo Jul 29 '12 at 18:34
@bobobobo I have checked around and found this ( that shouws that RAND_MAX is defined as 2147483647 and not 32768. I also found this post (…) that suggests to use arc4random(), although some suggest that cannot be always used as it can be slow (…). I tried arc4random and I have the impression that generates more evenly events so not quiet sure about the correctness of CCRANDOM – mm24 Jul 29 '12 at 18:54
up vote 4 down vote accepted

Yes, your general approach is fine (define a subset of some range as the "activation" range and produce a random number, if that number falls into the activation range you trigger something, otherwise you don't) -- provided your method of random number generation produces a sufficient distribution for your needs.

As is noted in the comments, there are various implementation details that could factor in to whether or not your RNG is sufficient.

This isn't directly related to your question, but this article on probability in games was linked recently in the #gamedev IRC channel on Afternet, and I thought you might find it interesting reading.

share|improve this answer
thanks a lot for your answer and especially for the great article. I was looking excactly for something like that as well as for the confirmation of my approach :) – mm24 Aug 1 '12 at 16:25

Since I can't comment, I have to point to people suggesting what RAND_MAX should be, that RAND_MAX is implementation defined, and you can easily determine it with a little program.

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char ** argv)
    printf("RAND_MAX = %i\n", RAND_MAX);
    return 1;

On my machine this gives RAND_MAX = 2147483647, much like what the gcc manual says. I would suggest an approach of

#define CCRANDOM_0_1() ((random() / (float)RAND_MAX ))

but actually, I would make this a function

float CCRANDOM_0_1() { return (random() /  (float)RAND_MAX) }

and also realize you probably want to seed the random number generator too. I wouldn't worry about the quality of your random number generator unless it becomes an apparent issue.

Back to your original question, I am not sure I fully understanding what you are trying to ask. Are you asking us if this is the best place to do this? They best way to do this? I think your code is generally fine, although I tend to be a "if it works" type programmer.

share|improve this answer
@Iaaph thanks a lot for your answer, it helps. Is am developing my first game so my question can be read as "Is this the best way to do this?". Thank u for your answer. I'll check out a couple of things and tomorrow I'll accept one of the two :) (both very helpful). – mm24 Jul 29 '12 at 21:04
@Iaaph, thanks a lot again.. I accepthed Josh answer for the nice article he linked, although, also some of your comments are very valuable and I appreciate them. Would be ideal to merge the two answers :). – mm24 Aug 1 '12 at 16:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.