Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 ))
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 (delorie.com/gnu/docs/glibc/libc_396.html) that shouws that RAND_MAX is defined as 2147483647 and not 32768. I also found this post (iphonedevelopment.blogspot.it/2008/10/…) that suggests to use arc4random(), although some suggest that cannot be always used as it can be slow (cocos2d-central.com/topic/…). 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
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

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.