0

I was wondering if this is the best way to generate a random number in Swift.

This is what I have thought of thus far:

var randomNumber:Int = random() * 100 + 1 //What value does this return?

I was wondering if this is a viable 1 - 100 range in Swift utilizing the random() function? I am unsure if this is. I have not dealt with random numbers much in Swift 2. However in Java, the equivalent would be Math.random() * 100 + 1

I'm curious to know what would be the equivalent of this in Swift 2. Thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Linuxn00b
  • 151
  • 2
  • 12
  • @EricD. yes, I flagged it but it didn't auto-generate the comment for some reason – Hamish Jan 31 '16 at 22:38
  • @originaluser2 I think it doesn't when it detects a comment with the link already in it, and there was one at this moment. – Eric Aya Jan 31 '16 at 22:39

1 Answers1

-1
let random = Int(arc4random_uniform(100) + 1)

+ 1 since arc4random_uniform generates a random number between 0 and the parameter - 1

belst
  • 2,285
  • 2
  • 20
  • 22