ios objective-c 配列をシャッフルする
iosアプリを開発中、配列をシャッフルしたくなった。
phpのshuffle()的な関数あるかと思いきや、
そんな便利関数はないようなので、下記のようにしている模様。
for (int i=0; i<[targetArray count]; i++) { int j = arc4random() % ([videos count]-1); [targetArray exchangeObjectAtIndex:i withObjectAtIndex:j]; }
arc4random()で乱数生成して、
[targetArray exchangeObjectAtIndex:i withObjectAtIndex:j];
でi番目とj番目の配列を入れ替えてる。
などほど・・・
感謝致します。