读书人

在开发程序中需要每次随机出现一组数

发布时间: 2012-11-25 11:44:31 作者: rapoo

在开发程序中,需要每次随机出现一组数据的顺序下面是随机数算法

NSArray *temp = [NSArrayarrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", nil];

NSMutableArray *tempArray = [[NSMutableArrayalloc] initWithArray:temp];

NSMutableArray *resultArray = [[NSMutableArrayalloc] init];

int i;

int count = temp.count;

for (i = 0; i < count; i ++) {

int index = arc4random() % (count - i);

[resultArray addObject:[tempArray objectAtIndex:index]];

[tempArray removeObjectAtIndex:index];

}

[tempArray release];

NSLog(@"resultArray is %@",resultArray);

NSLog(@"看看结果楼");

读书人网 >移动开发

热点推荐