這裏是我的代碼:可可觸摸:RAND()返回相同的字符串
-(void)randommoves
{
NSArray *possiblemoves =[NSArray arrayWithObjects:@"R ",@"R' ",@"L ",@"L' ",@"B ",@"B' ",@"F ",@"F' ",@"U ",@"U' ",@"D ",@"D' ", nil];
NSMutableString *finalmoves = [[NSMutableString alloc] init];
finalmoves = [NSMutableString stringWithCapacity:0];
[finalmoves retain];
int i = 0;
for (i=0; i<20; i++) {
int r = rand() % 13;
NSString *string = [possiblemoves objectAtIndex:r];
[finalmoves appendString:string];
}
NSLog(@"%@",finalmoves);
[finalmoves release];
}
而且每次運行它的時候,我得到了完全相同的字符串「d‘BB’DL‘d’F‘L’B 「U」 DDD「L」 URBFD「B」「
我希望它做的是每次給我一個新的移動設置我運行它
我跑這至少30倍,以確保它不是一個僥倖,它真的是返回相同的字符串,果然,它是。
謝謝,它的工作(也發現了一個小錯誤,我在int r = rand()%13;我忘了計數0) – 2010-02-07 03:16:58