I am new to Swift. Trying this code in Playground and get the error (see description below) Can you, please, point me to the right direction - where to look for the solution? Thanks in advance.
func randomSet(num: Int, max: Int) -> Array<Double> {
var randArray = Array<Double>()
for index in 0...num {
randArray[index] = Double(arc4random_uniform(max+1))
}
ERROR: var sum = randArray.reduce(0) {$0 + $1}
for index in 0...num {
randArray[index] = randArray[index] / Double(sum) * Double(max)
}
return randArray
}
test = randomSet(10, 100)
On the line marked with word ERROR, I get this:
Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
0...numincludes the range end, so that will give you an array withnum+1elements. You probably want to0 ..< numinstead.