Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何制作随机浮动?_Ios_Xcode_Swift - Fatal编程技术网

Ios 如何制作随机浮动?

Ios 如何制作随机浮动?,ios,xcode,swift,Ios,Xcode,Swift,我现在有一行代码,可以创建随机数1-5,但如何包含小数,1.00-5.00 stoptimer.text = String(arc4random_uniform(4)+1) 将生成的数字范围从100增加到500,并将生成的数字除以100 stoptimer.text = String((arc4random_uniform(400)+100) / 100) 您可以将Int转换为Double,并使用String(format:)方法根据需要格式化字符串: stoptimer.text = S

我现在有一行代码,可以创建随机数1-5,但如何包含小数,1.00-5.00

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

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

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

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

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

我想这个链接可能会帮助你:[双精度浮点随机数][1][1]:你不想要随机浮点,你想要一个随机整数转换成浮点。这并不完全相同。请注意,arc4random_uniform不会将上限值包含在返回的值范围内(它将在[1.00-5.00[)。请确保您知道。我不想要1.34或4.54。我只想要1.00、2.00、3.00、4.00、5.00