通过数字范围随机化 [英] Randomizing through number range

查看:77
本文介绍了通过数字范围随机化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使应用程序的这一部分正常工作,即用户单击一个按钮,并且标签会打印一个介于1到12之间的随机生成的数字。我已经能够成功地做到这一点,但我也希望它不要重复已经打印的任何随机数。

I'm trying to get this part of the app to work were the user clicks a button and a label prints a randomly generated number between 1-12. I've been able to successfully do that, but I also want it to not repeat any random numbers that have already been printed.

我尝试做的是放入将任何打印的数字放入数组中,然后在每次生成新数字时检查该数组。

What I've tried doing is putting any printed number into an array, and then checking the array each time it generates a new number.

我已经将其在游乐场中使用,但无法获取

I've gotten it to work in the Playground, but cannot get it working with a real project.

这是我的项目的代码。

    var usedNumbers = [Int]()
    var randomConv = 0

func randomize() {
    lblRandom.text = "\(arc4random_uniform(12) + 1)"
    randomConv = Int(lblRandom.text!)!
}

@IBAction func btnRandomPressed(sender: AnyObject) {
    randomize()
    if usedNumbers.contains(randomConv) {       
        randomize()

    } else {    
        usedNumbers.append(randomConv)

    }

    if usedNumbers.count == 12 {
        btnRandom.hidden = true
    } 
}

这是我的代码操场。

var lblRandom = "\(arc4random_uniform(12) + 1)"
var randomConv = 0
var usedNumbers = [Int]()

func randomize() {
lblRandom = "\(arc4random_uniform(12) + 1)"
randomConv = Int(lblRandom)!
}

repeat {
randomize()

if usedNumbers.contains(randomConv) {
    randomize()   
} else {
    usedNumbers.append(randomConv)
    print(lblRandom)
}

} while usedNumbers.count < 12


推荐答案

我不太确定为什么您的代码是

I´m not quite sure why your code is not working, but try this code instead this works.

var usedNumbers = [Int]()
var randomConv = 0

@IBAction func btnRandomPressed(sender: AnyObject) {
     randomConv = Int(arc4random_uniform(12) + 1)

        if usedNumbers.contains(randomConv) {
            // If you find a duplicate you fire the event again and a new number will be randomized
            print("Exists \(randomConv)")
            btn_Pressed.sendActionsForControlEvents(.TouchUpInside)

        } else {
            print(randomConv)
            lblTest.text = String(randomConv)
            usedNumbers.append(randomConv)

       }
}

更新

usedNumbers.contains(randomConv)条件为 true ,您可以使用此行来触发再次执行按钮事件: btn_Pressed.sendActionsForControlEvents(.TouchUpInside)-btn_Pressed是您的故事板中的按钮出口。

Update
When the usedNumbers.contains(randomConv) condition is true, you can use this row to fire the button event again: btn_Pressed.sendActionsForControlEvents(.TouchUpInside) - btn_Pressed is your buttons outlet from your Storyboard.

我已经更新了代码块,以便您可以看到完整的示例。

I have updated the code block so that you can see a fully working example.

更新2,替代解决方案

func randomize(){
    repeat {
        if (usedNumbers.count == 12){
            return
        }
        randomConv = Int(arc4random_uniform(12) + 1)
    } while usedNumbers.contains(randomConv)

    usedNumbers.append(randomConv)
    lblTest.text = String(randomConv)
}

@IBAction func btnRandomPressed(sender: AnyObject) {
     randomize()
}

这篇关于通过数字范围随机化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆