开发者

CCRandom_0_1() generate the same number

i use CCRandom_0_1() to generate a random number, as suggested in the book 'learn-iphone-and-ipad-with-cocos2d' but it seems the function generates the same number.

    int ran1 = ((int) (CCRANDOM_0_1() * 5)) + 1;
    int ran2 = ran1;
    while (ran2 == ran1) {
        ran2 = ((int) (CCRANDOM_0_1() * 5)) + 1;
    }

here is the code where i want to generate 2 different integer from 1 to 5. but the console is always

2011-05-28 14:57:56.699 LetsSpotIt[2443:707] r1: 4.200939 r1: 1.971915

Is there anyway i can seed it according to time(mini second something), or is there any other function开发者_运维技巧s to use?

Please give me example code. I didn't learn c or c++ before. Thank you.


If you want different values every time you use the code, then you must seed the random generator. The most popular way is to call srandom(time(NULL)) before using CCRANDOM


Have you tried arc4random() function? Also look here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜