LoginSignup
24
23

More than 5 years have passed since last update.

Swiftで乱数を発生させる

Last updated at Posted at 2014-07-13

arc4random

やり方はObjective-Cと変わらず、arc4random関数を使って乱数を発生させられます。
今回はとりあえず1〜100の間で乱数を発生させます。

コード


for var i = 0; i <= 4; i++ {
            var n = arc4random() % 100 + 1;

            NSLog("\(i) : \(n)")
        }

結果

2014-07-13 22:40:56.669 TestSource[910:15388] 0 : 99
2014-07-13 22:40:56.670 TestSource[910:15388] 1 : 53
2014-07-13 22:40:56.670 TestSource[910:15388] 2 : 14
2014-07-13 22:40:56.671 TestSource[910:15388] 3 : 52
2014-07-13 22:40:56.671 TestSource[910:15388] 4 : 87

24
23
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
24
23