LoginSignup
5
4

More than 5 years have passed since last update.

swiftでランダム変数

Posted at

ほんと何番煎じかわからないけど、、
ランダム変数生成して判定するってのを書いてみました。

class SecondViewController: UIViewController {
    @IBOutlet var message : UILabel
    @IBAction func actionButton(sender : AnyObject) {
        let randInt = arc4random_uniform(10);
        if(randInt <= 5) {
            message.text = "hoge"
        } else {
            message.text = "moge"
        }
    }   
}

画面上にラベルとボタンを設置して
ラベル -> @IBOutlet var message : UILabel
ボタン -> @IBAction func actionButton
と、つなげる。

ボタンを押すと、0~10までのランダム変数がarc4random_uniform(10)で生成されて、
生成された変数の値が5以下であればhogeそうでなければmogeがラベルに表示される。

そんだけ。
ほとんどswift感ないけど、、、。

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