Force Shuffle NSMutableArray

前端 未结 6 964
深忆病人
深忆病人 2021-01-13 14:03

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

6条回答
  •  臣服心动
    2021-01-13 14:43

    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.

提交回复
热议问题