-3

I currently have a line of code which creates random numbers 1-5, but how do I include decimals, 1.00-5.00?

 stoptimer.text = String(arc4random_uniform(4)+1)
tanman
  • 175
  • 10
  • 1
    I think this link may help you: [double and float random numbers][1] [1]: http://stackoverflow.com/a/28075271/35499 – Dean Jun 17 '15 at 20:56
  • 1
    You don't want a random float, you want a random int converted to float. That's not exactly the same. – Cyrille Jun 17 '15 at 21:24

2 Answers2

2

You can convert your Int to Double and use String(format:) method to format your string as desired:

stoptimer.text = String(format: "%.2f", Double(arc4random_uniform(5)+1))
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
1

Increase the range of your generated numbers from 100 to 500 and devide the resulting one by 100.

stoptimer.text = String((arc4random_uniform(400)+100) / 100)
SUhrmann
  • 632
  • 1
  • 8
  • 26
  • 1
    Note that arc4random_uniform won't include the upper bound value in the range of values returned ( it will be in the range [1.00-5.00[ ). Just making sure you know that. – John Difool Jun 17 '15 at 21:02
  • I don't want 1.34 or 4.54. I only want 1.00, 2.00, 3.00, 4.00, 5.00 – tanman Jun 17 '15 at 21:05