我正在使用arc4random_uniform()对数组进行洗牌,如下所示:
int count = [arr count];
for (int i = 0; i < count; i++) {
int n = arc4random_uniform(count - i) + i;
[arr exchangeObjectAtIndex:i withObjectAtIndex:n];
}
该阵列包含32张扑克牌,许多用户报告他们反复看到相同的卡片序列。创建阵列的过程如下:
我从文档中了解到arc4random_uniform不需要播种。所以我的问题是:
答案 0 :(得分:0)
我将您的代码粘贴到我正在处理的应用中。
例程被称为x次:
for (int i = 0; i < 5; i++) {
[self trySort];
}
例程加载数组,对其进行排序,然后打印出来:
- (void)trySort {
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", nil];
//code pasted from question
int count = [arr count];
for (int i = 0; i < count; i++) {
int n = arc4random_uniform(count - i) + i;
[arr exchangeObjectAtIndex:i withObjectAtIndex:n];
}
//
NSLog(@"%@%@%@%@%@%@%@%@%@%@%@%@", [arr objectAtIndex:0], [arr objectAtIndex:1], [arr objectAtIndex:2], [arr objectAtIndex:3], [arr objectAtIndex:4], [arr objectAtIndex:5], [arr objectAtIndex:6], [arr objectAtIndex:7], [arr objectAtIndex:8], [arr objectAtIndex:9], [arr objectAtIndex:10], [arr objectAtIndex:11]);
}
结果合理分类:
2013-04-07 16:24:50.923 xxx[2122:c07] BIAKHFDEGCLJ
2013-04-07 16:24:50.927 xxx[2122:c07] DGICJHLBAKFE
2013-04-07 16:24:50.928 xxx[2122:c07] HIGEFADJLCKB
2013-04-07 16:24:50.928 xxx[2122:c07] IHFDBJEALCKG
2013-04-07 16:24:50.928 xxx[2122:c07] GBEHIFCKAJDL
在加载数组的时间和排序时间之间是否有可能出现阻止排序的事情?