我使用CCRandom_0_1()生成一個隨機數,如書籍'learn-iphone-and-ipad-with-cocos2d' 中所建議的,但似乎該函數生成相同的數字。CCRandom_0_1()生成相同的數字
int ran1 = ((int) (CCRANDOM_0_1() * 5)) + 1;
int ran2 = ran1;
while (ran2 == ran1) {
ran2 = ((int) (CCRANDOM_0_1() * 5)) + 1;
}
這裏就是我要生成到5 1 2個不同的整數代碼,但在控制檯始終
2011-05-28 14:57:56.699 LetsSpotIt[2443:707] r1: 4.200939 r1: 1.971915
反正是有,我可以根據時間播種它(迷你的第二個東西),還是有任何其他功能使用?
請給我示例代碼。我以前沒有學過c或C++。謝謝。
nope之前調用srandom(time(NULL)),但我認爲它們或多或少是相同的。所以我認爲我會遇到同樣的問題,每個隨機功能 – OMGPOP 2011-05-28 07:17:41
是的,這個功能是偉大的。但是如果我想使用float或double而不是整數? – OMGPOP 2011-05-28 07:27:00
有趣。我GOOGLE了它,並發現http://stackoverflow.com/questions/1131101/whats-wrong-with-this-randomize-function和http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs -arc4random.html – 2011-05-28 07:41:11