Skip to content

Tag: float

Random float and integer in Swift

Getting a random number within a range is a very common operation. There are multiple ways of extending Swift language to add support for getting random values within a specified range. After experimenting with different implementations like utility functions, integer and float extensions I have found that the most natural way of doing it is to extend ClosedRange. This will allow to write a very readable code: Random floating point values 1. Get a random value using a max possible range (note that arc4random_uniform excludes the upper bound). 2. Convert… Read more Random float and integer in Swift