6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

範囲を指定した乱数生成のメモメモ

random()

int min = 5
int max = 10
srand((unsigned)time(NULL));
int rNumber = (random() % (max + 1 - min)) + min;

シードを与える必要があります。

arc4random()

int min = 5
int max = 10
int rNumber = (arc4random() % (max + 1 - min)) + min;

一部条件下で偏りがあるらしいです。

arc4random_uniform(n)

int min = 5
int max = 10
int rNumber = arc4random_uniform(max + 1 - min) + min;

arc4random() の改善版?

NSMutableArrayをランダムに入れ替える

NSMutableArray *mArray;

for (int i = 0; i < [mArray count]; i++) {
    int n = arc4random_uniform([mArray count] - i) + i;
    [mArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?