Re-arrange NSArray/MSMutableArray in random order

后端 未结 4 1432
余生分开走
余生分开走 2020-12-01 11:32

I\'m using NSArray/NSMutable array to store different objects. Once all the objects are added, I want to re-arrange them in random order. How can I do it?

4条回答
  •  攒了一身酷
    2020-12-01 11:45

    // the Knuth shuffle
    for (NSInteger i = array.count-1; i > 0; i--)
    {
        [array exchangeObjectAtIndex:i withObjectAtIndex:arc4random_uniform(i+1)];
    }
    

提交回复
热议问题