LoginSignup
4
4

More than 5 years have passed since last update.

【Swift】乱数を発生させfor文で任意数を配列に追加

Posted at

乱数を発生させfor文で任意数を配列に追加

コード

// 空の配列を宣言
var Array: [Int] = []

for (var i = 0; i < 10; i++) {

    //乱数を生成
    var rand = Int(arc4random_uniform(UInt32(10)))

    //乱数を配列に格納
    Array.append(rand)
}

//配列を表示
println(Array)

結果

[6, 8, 1, 0, 5, 4, 0, 3, 3, 9]

メモ

arc4randomの戻り値がUInt32なので最大数値の値も合わせる必要があった。

参考

[Swift] 乱数取得でキャストにハマったのでメモ(arc4random)
http://qiita.com/m-tsuchiya/items/37c0c1b52f0dd70e190d

4
4
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
4
4