我正在尝试在我的应用程序中洗牌,并使用以下代码。这会充分随机化甲板吗?我几乎可以肯定只是想要另一种意见。谢谢!for (int i = 0; i < 40000; i++) { int randomInt1 = arc4random() % [deck.cards count]; int randomInt2 = arc4random() % [deck.cards count]; [deck.cards exchangeObjectAtIndex:randomInt1 withObjectAtIndex:randomInt2];编辑:以防万一有人想知道或将来应该遇到这个问题。这就是我用来洗牌的方法,它是 Fisher-Yates 算法的实现。我从下面建议的@MartinR 帖子中得到它,可以在这里找到:What's the Best Way to Shuffle an NSMutableArray?NSUInteger count = [deck.cards count]; for (uint i = 0; i < count; ++i) { // Select a random element between i and end of array to swap with. int nElements = count - i; int n = arc4random_uniform(nElements) + i; [deck.cards exchangeObjectAtIndex:i withObjectAtIndex:n]; } 最佳答案 如果 [deck.cards count] for (int i = [deck.cards count] - 1; i > 0 ; i--) { int randomInt1 = arc4random_uniform(i + 1); [deck.cards exchangeObjectAtIndex:randomInt1 withObjectAtIndex:i];}来自文档: arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.关于ios - 这是洗一副牌的充分方法吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23393589/
10-12 15:05