I'm using random() to generate random numbers triggering certain events in my app. The only thing is, it is not random, meaning the same event will always get triggered first each time I start my app. So random() does not seem random... Is there another way to get a true random number?
Random question...
(8 posts) (7 voices)-
Posted 2 years ago #
-
Put
srandom(time(NULL));
somewhere before you call random...Also there are two predefined Cocos2d random functions,
CCRANDOM_MINUS1_1()andCCRANDOM_0_1()you can use...Posted 2 years ago # -
If you post code it would probably be easier to know your exact problem. Often times (in other languages) people have problems when they use a double or floating point generator that produces numbers between 0 and 1. They will usually cast this to an integer which then truncates the decimal which will virtually never produce a 1 but will almost always get 0.
Posted 2 years ago # -
Use
arc4random()instead ofrandom().Posted 2 years ago # -
+1 to clain. arc4random() provides much better random results and does not require seeding.
Posted 2 years ago # -
This blog will hopefully be helpful for you:
http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs-arc4random.htmlPosted 2 years ago # -
Thanks all, I went with arc4random() which is giving far better results
Posted 2 years ago # -
Just to note, the article is wrong in using 0x10000000 as the max value of a float/int32. That's the number of possible values, -1 for the max value (don't forget our 0). Use this instead:
#define CCRANDOM_X_Y(__X__, __Y__) (((__Y__) - (__X__)) * (arc4random() / (float)0xffffffff) + (__X__))A trivial point.
Posted 1 month ago #
Reply
You must log in to post.