dimanche 8 mars 2015

The code is right but sometimes i got a runtime error

I write a function,inside this function there is a switch and in every case there are operations with random generated numbers I call this function inside a for cycle for many times and copy the results into an array,and sometimes i get a EXC_BAD_Instruction error during execution, I don't understand why


I notice that if the cycle is longer the error show up more often,why? What i have to do to use this code inside a bigger cycle without any error?



var operationNumber = random() % 4

switch operationNumber {
case 0:
number1 = random() % (biggestN + 1)
number2 = random() % (biggestN + 1)
result = number1 + number2
operation = "+"
case 1:
number1 = random() % (biggestN + 1)
number2 = random() % (number1 + 1)
result = number1 - number2
operation = "-"
case 2:
number1 = (random() % (biggestN + 1))+1
number2 = (random() % (number1/2))+1
result = number1 * number2
operation = "x"
case 3:
result = random()%biggestN
number1 = (random()%biggestN)*result
number2 = number1 / result
operation = "/"
default:
break
}


In the editor the line with the error is the one with case 0,so I think that the error is inside the switch


Thanks for the answers





Aucun commentaire:

Enregistrer un commentaire