xcode 强制洗牌 NSMutableArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11224014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:42:11  来源:igfitidea点击:

Force Shuffle NSMutableArray

objective-ciosxcode

提问by Alex G

I have a NSMutableArray called putNumberUsed. It contains the following objects @"blah1,@"blah2",@"blah3",@"blah4". I want to shuffle these objects randomly so for example if I chose:

我有一个名为 putNumberUsed 的 NSMutableArray。它包含以下对象@"blah1,@"blah2",@"blah3",@"blah4"。我想随机调整这些对象,例如,如果我选择:

 [putNumberUsed objectAtIndex:0] 

it would give me anything but "blah1". How would I go about doing this? The following is the code I used thus far:

它会给我除了 "blah1" 之外的任何东西。我该怎么做呢?以下是我迄今为止使用的代码:

NSMutableArray *putNumbersUsed = [[NSMutableArray alloc] arrayWithObjects:@"blah1",@"blah2",@"blah3",@"blah4",nil];

回答by Rajesh

I think, You can write a loop for that. Please check the following code,

我认为,您可以为此编写一个循环。请检查以下代码,

for (int i = 0; i < putNumberUsed.count; i++) {
    int randomInt1 = arc4random() % [putNumberUsed count];
    int randomInt2 = arc4random() % [putNumberUsed count];
    [putNumberUsed exchangeObjectAtIndex:randomInt1 withObjectAtIndex:randomInt2];
}

I this this may be useful to you.

我这可能对你有用。

回答by Mukesh

From iOS 10.x++ new concept of shuffle arrayis given by Apple,

从 iOS 10.x++ 开始,Apple 给出了shuffle 数组的概念

You need to import the framework :

您需要导入框架:

ObjeC

对象C

#import <GameplayKit/GameplayKit.h>

NSArray *shuffledArray = [yourArray shuffledArray];

Swift

迅速

import GameplayKit

let shuffledArray = yourArray.shuffled()

回答by C?ur

Here is a shuffling solution with all positions forced to change when count > 1.

这是一个改组解决方案,当计数 > 1 时,所有位置都被迫改变。

Add a category like NSMutableArray+Shuffle.m:

添加类似 NSMutableArray+Shuffle.m 的类别:

@implementation NSMutableArray (Shuffle)
// Fisher-Yates shuffle variation with all positions forced to change
- (void)unstableShuffle
{
    for (NSInteger i = self.count - 1; i > 0; i--)
        // note: we use u_int32_t because `arc4random_uniform` doesn't support int64
        [self exchangeObjectAtIndex:i withObjectAtIndex:arc4random_uniform((u_int32_t)i)];
}
@end

Then you can shuffle like:

然后你可以像这样洗牌:

[putNumbersUsed unstableShuffle];

This solution:

这个解决方案:

A Swift 3.2 and Swift 4 equivalent is:

Swift 3.2 和 Swift 4 等效项是:

extension Array {
    mutating func unstableShuffle() {
        for i in stride(from: count - 1, to: 0, by: -1) {
            swapAt(i, Int(arc4random_uniform(UInt32(i))))
        }
    }
}

A Swift 3.0 and 3.1 equivalent is:

Swift 3.0 和 3.1 等效项是:

extension Array {
    mutating func unstableShuffle() {
        for i in stride(from: count - 1, to: 0, by: -1) {
            swap(&self[i], &self[Int(arc4random_uniform(UInt32(i)))])
        }
    }
}

Note: An algorithm for regular shuffling (where a result with same positions being possible) is also available

注意:也可以使用用于常规洗牌的算法(其中可能有相同位置的结果)

回答by Rajesh

You can shuffle the object by using the following line of code,

您可以使用以下代码行来洗牌对象,

[putNumbersUsed exchangeObjectAtIndex:3 withObjectAtIndex:0];

I think this may useful to you.

我认为这可能对你有用。

回答by janusfidel

generate a random number for index

为索引生成一个随机数

int randomInt = arc4random() % [putNumberUsed count];
[putNumberUsed objectAtIndex:randomInt];

回答by Fabio Poloni

Use this:

用这个:

for (int i = 0; i < [putNumberUsed count]; i++) {
    int random = arc4random() % [putNumberUsed count]; 
    [putNumbersUsed exchangeObjectAtIndex:random withObjectAtIndex:i]; 
}