我如何制作随机浮标?

时间:2015-06-17 20:50:30

标签: ios xcode swift

我目前有一行代码创建随机数1-5,但如何包含小数,1.00-5.00?

 stoptimer.text = String(arc4random_uniform(4)+1)

2 个答案:

答案 0 :(得分:2)

您可以将Int转换为Double并使用String(format :)方法根据需要格式化字符串:

stoptimer.text = String(format: "%.2f", Double(arc4random_uniform(5)+1))

答案 1 :(得分:1)

将生成的数字范围从100增加到500,并将结果数字除以100。

stoptimer.text = String((arc4random_uniform(400)+100) / 100)