John White John White - 5 months ago 75
Swift Question

min min: CGFloat, max: CGFloat

func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
// here
func random(min min: CGFloat, max: CGFloat) -> CGFloat {
return random() % (max - min) + min
}


Why I should write two times 'min' in the second function?
If I won't do this I am unable to call second function with code:

random(min: 1, max: 5);

Answer

Remove second min from function. By the way when you call a function, skip the first variable name and just put the value. It's the swift syntax for calling a function

  func random(min: CGFloat, max: CGFloat) -> CGFloat {
      return random() % (max - min) + min
  }

  func random() -> CGFloat {
      return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
  }

  random(1, max: 5)
Comments