我使用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