0

I am getting the error, "Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)" when trying to append random array elements into a new array.

The debug log says "fatal error: Index out of range"

//If there are more than 6 players prioritizing the event, make a random choice. garudaLocations is an array containing the players who prioritized the event "Garuda".

    if garudaLocations.count > 6 {

        var finalGarudaPlayers : [Int] = []
        let getRandom = randomSequenceGenerator(1, max: garudaLocations.count) //Tell RNG how many numbers it has to pick from.
        var randomGarudaPrioritiesIndex = Int()
        for _ in 1...6 {
            randomGarudaPrioritiesIndex = getRandom() //Generate a random number.
            finalGarudaPlayers.append(garudaLocations[randomGarudaPrioritiesIndex]) //ERROR: Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)
        }
        debugPrint(finalGarudaPlayers) //Print array with the final priority Garuda members.

randomSequenceGenerator is a function I got from here, which does work to generate the random numbers.

 func randomSequenceGenerator(min: Int, max: Int) -> () -> Int {
    var numbers: [Int] = []
    return {
        if numbers.count == 0 {
            numbers = Array(min ... max)
        }

        let index = Int(arc4random_uniform(UInt32(numbers.count)))
        return numbers.removeAtIndex(index)
    }
}

To get a better understanding, I am trying to write a piece of a "team making" program where players are automatically sorted into events, but they can choose which events they would like to prioritize.

I can only have 6 people per event, however, so the goal is to take the existing garudaLocations array, choose a random 6 index locations, and get rid of the rest of the players.

I get the error only after I submit more than 6 players to the same event.

Any help is very much appreciated!

Community
  • 1
  • 1

1 Answers1

1

You can never speak of a non-existent index. If you do, you will crash just as you are crashing now.

So, you are saying:

garudaLocations[randomGarudaPrioritiesIndex]

Now, I do not know what garudaLocations is. But I can tell you for certain that if randomGarudaPrioritiesIndex is not an existing index within garudaLocations, you will absolutely crash.

Thus, you can easily debug this by logging (print) randomGarudaPrioritiesIndex.

Bear in mind that the largest existing index is not garudaLocations[garudaLocations.count]. It is garudaLocations[garudaLocations.count-1]. So compare randomGarudaPrioritiesIndex to garudaLocations.count-1. If it is greater, you crash when you use it as an index on garudaLocations.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • garudaLocations is the array that contains the players who chose the event as a priority. randomGarudaPrioritiesIndex is meant to be a variable that holds a random number (generated with the randomSequenceGenerator function). Normally to list a *specific* Index element, you would use (array[X]) to list element X in the array, for example. I thought that I would be able to use the random number generator in the place of the 0 so that I could find a *random* array element. Is that not the case? – Collin Stover Sep 23 '16 at 00:31
  • and when I debugPrint(randomGarudaPrioritiesIndex) I get 6 random Integers, as expected (not in an array). – Collin Stover Sep 23 '16 at 00:39
  • All of that is great. But it seems irrelevant; the question is whether any of those random integers is legal to use in the expression `garudaLocations[randomGarudaPrioritiesIndex]`. Clearly, sometimes it isn't. – matt Sep 23 '16 at 01:08
  • I totally understand what you are talking about., now. I changed **let getRandom = randomSequenceGenerator(1, max: garudaLocations.count)** to **let getRandom = randomSequenceGenerator(0, max: garudaLocations.count-1)** and everything works perfectly now. I see now what you were saying...because random number starts at 1 and counts all the way up to the array.count, if it generated the top array number, it didn't exist in the array (since arrays start at 0). Thank you for being patient. – Collin Stover Sep 23 '16 at 19:16
  • No problem. That's why I stressed the notion of `count-1` in my answer! – matt Sep 23 '16 at 21:46