I have an array with 7 objects inside it, and am using the Array.count
to get the number of objects inside the array.
I'm then using Int.random(in: 0 ... arrayMax)
to get a random object from the array. Yet, because there are 7 objects in the array, and the .count
will return 7, I'll every once in a while get an error, because 7
is obviously 6
.
I've tried to solve this by going like this:
let nTMax = Array.count
let nTSelection = Int.random(in: 0 ... nTMax)
print (nTMax, nTSelection)
let nTSelectionProtect = nTSelection -1
but the nTSelectionProtect
refuses to be accepted. I was hoping to do a safety calculation so that 7 will always be 6, but I'm not sure how to do it.
How would I do it?