The method I've devised so far is this:
func randRange (lower : Int , upper : Int) -> Int {
let difference = upper - lower
return Int(Float(rand())/Float(RAND_MAX) * Float(difference + 1)) + lower
}
This generates random integers between lower and upper inclusive.
If you are into extensions:
Edited to remove modulo bias per the suggestion in comments. (thanks!)
I think a neat way of doing this may be to use Swift's Range to define the bounds because then you can specify 1..100 or 1...100 (including or excluding the upper bound). The best I have come up with so far is:
I tried using an extension on Range but you cannot seem to extend Range< T where T: Int > specifically. It would be even better if you could get a syntax like (1..100).rand().
Here's a somewhat lighter version of it:
This can be simplified even further if you decide this function works with unsigned values only:
Or, following Anton's (+1 for you) excellent idea of using a range as parameter: