OMGPOP
OMGPOP

Reputation: 952

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 functions to use?

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

Upvotes: 1

Views: 5457

Answers (2)

cpprulez
cpprulez

Reputation: 896

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

Upvotes: 6

user745098
user745098

Reputation:

Have you tried arc4random() function? Also look here

Upvotes: 4

Related Questions