我將你的代碼粘貼到我正在處理的應用程序中。
程序被調用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
是它可能在你的數組被加載的時間和它被排序的時間之間,有些事情會阻止這樣做RT?
謝謝瑞克。我同意你的看法,我也不能重現這個問題,這就是爲什麼我問什麼會導致問題發生。他們說卡片實際上總是被洗牌,但洗牌的結果本身會出現多次。例如,如果洗牌的結果是序列DEFGRSKL,他們會在短時間內看到它不止一次。 – Motasim 2013-04-08 04:37:03