如何随机化 NSMutableArray?

发布于 2024-11-13 20:43:12 字数 662 浏览 1 评论 0原文

可能的重复:
iPhone - nsarray/nsmutablearray - 以随机顺序重新排列

我有一个包含 20 个对象的 NSMutableArray。有什么方法可以让我随机排列他们的顺序,就像你洗牌一样。 (我所说的顺序是指它们在数组中的索引)

就像我有一个包含以下内容的数组:

  1. apple
  2. orange
  3. pear
  4. banana

我怎样才能随机化顺序,这样我就可以得到类似的东西:

  1. 橙色
  2. 苹果
  3. 香蕉

Possible Duplicate:
iphone - nsarray/nsmutablearray - re-arrange in random order

I have an NSMutableArray that contains 20 objects. Is there any way that I could randomize their order like when you shuffle a deck. (By order I mean their index in the array)

Like if I had an array that contained:

  1. apple
  2. orange
  3. pear
  4. banana

How could I randomize the order so that I could get something like:

  1. orange
  2. apple
  3. banana
  4. pear

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-11-20 20:43:12

这是一些示例代码:
迭代数组,并随机交换一个对象与另一个对象的位置。

for (int x = 0; x < [array count]; x++) {
    int randInt = (arc4random() % ([array count] - x)) + x;
    [array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}

Here's some sample code:
Iterates through the array, and randomly switches an object's position with another.

for (int x = 0; x < [array count]; x++) {
    int randInt = (arc4random() % ([array count] - x)) + x;
    [array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
傲世九天 2024-11-20 20:43:12
@interface NSArray (Shuffling)

- (NSArray *)shuffledArray;

@end

@implementation NSArray (Shuffling)

- (NSArray *)shuffledArray {
    NSMutableArray *newArray = [[self mutableCopy] autorelease];

    [newArray shuffle];
    return newArray;
}

@end

@interface NSMutableArray (Shuffling)

- (void)shuffle;

@end

@implementation NSMutableArray (Shuffling)

- (void)shuffle {
    @synchronized(self) {
        NSUInteger count = [self count];

        if (count == 0) {
            return;
        }

        for (NSUInteger i = 0; i < count; i++) {
            NSUInteger j = arc4random() % (count - 1);

            if (j != i) {
                [self exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
}

@end

但请记住,这种改组是只是伪随机洗牌

@interface NSArray (Shuffling)

- (NSArray *)shuffledArray;

@end

@implementation NSArray (Shuffling)

- (NSArray *)shuffledArray {
    NSMutableArray *newArray = [[self mutableCopy] autorelease];

    [newArray shuffle];
    return newArray;
}

@end

@interface NSMutableArray (Shuffling)

- (void)shuffle;

@end

@implementation NSMutableArray (Shuffling)

- (void)shuffle {
    @synchronized(self) {
        NSUInteger count = [self count];

        if (count == 0) {
            return;
        }

        for (NSUInteger i = 0; i < count; i++) {
            NSUInteger j = arc4random() % (count - 1);

            if (j != i) {
                [self exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
}

@end

However keep in mind that this kind of shuffling is merely pseudorandom shuffling!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文