Vectors breaking when printing

When I access an index in a vector, write to it, and read it back, it is correct. But when I print the vector, it explodes.

The code is:

struct card
    suit
    number
end

function generate_deck(number_of_suits::UInt8, size_of_suits::UInt8)::Array
    deck = []

    for suit in 1:number_of_suits
        for number in 1:size_of_suits
            push!(deck, card(suit, number))
        end
    end

    return deck
end

function uniformity_index(deck::Array, number_of_suits::UInt8, size_of_suits::UInt8)::Float64
    optimal_distance_suits = number_of_suits
    optimal_distance_numbers = size_of_suits
    distances_suits = Vector{Int}(undef, (number_of_suits * (size_of_suits - 1)))
    distances_numbers = Vector{Int}(undef, (size_of_suits * (number_of_suits - 1)))

    Threads.@threads for suit in 1:number_of_suits
        last_card_index = 0
        for number in 1:size_of_suits
            step_from_last_card = 1
            found = false
            while !found
                if last_card_index == 0
                    if deck[step_from_last_card].suit == suit
                        last_card_index = step_from_last_card
                        found = true
                    end
                else
                    if deck[last_card_index+step_from_last_card].suit == suit
                        distances_suits[suit+number] = round(step_from_last_card) 
                        last_card_index += step_from_last_card
                        found = true
                        println(distances_suits[suit+number])
                    end
                end

                step_from_last_card += 1
            end
        end
    end

    Threads.@threads for number in 1:size_of_suits
        last_card_index = 0
        for suit in 1:number_of_suits
            step_from_last_card = 1
            found = false
            while !found
                if last_card_index == 0
                    if deck[step_from_last_card].number == number
                        last_card_index = step_from_last_card
                        found = true
                    end
                else
                    if deck[last_card_index+step_from_last_card].number == number
                        distances_numbers[number+suit] = round(step_from_last_card)
                        last_card_index += step_from_last_card
                        found = true
                        println(distances_numbers[suit+number])
                    end
                end

                step_from_last_card += 1
            end
        end
    end
    
    println(distances_suits, "\n")
    println(distances_numbers, "\n")

    suit_index = (sum(distances_suits) / length(distances_suits)) / optimal_distance_suits
    number_index = (sum(distances_numbers) / length(distances_numbers)) / optimal_distance_numbers

    return suit_index * number_index
end

const number_of_suits::UInt8 = 4
const size_of_suits::UInt8 = 13

deck = generate_deck(number_of_suits, size_of_suits)
println(deck, "\n")
index = uniformity_index(deck, number_of_suits, size_of_suits)
println(index)

the output:

Any[card(1, 1), card(1, 2), card(1, 3), card(1, 4), card(1, 5), card(1, 6), card(1, 7), card(1, 8), card(1, 9), card(1, 10), card(1, 11), card(1, 12), card(1, 13), card(2, 1), card(2, 
2), card(2, 3), card(2, 4), card(2, 5), card(2, 6), card(2, 7), card(2, 8), card(2, 9), card(2, 10), card(2, 11), card(2, 12), card(2, 13), card(3, 1), card(3, 2), card(3, 3), card(3, 
4), card(3, 5), card(3, 6), card(3, 7), card(3, 8), card(3, 9), card(3, 10), card(3, 11), card(3, 12), card(3, 13), card(4, 1), card(4, 2), card(4, 3), card(4, 4), card(4, 5), card(4, 
6), card(4, 7), card(4, 8), card(4, 9), card(4, 10), card(4, 11), card(4, 12), card(4, 13)]

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
[140089609474016, 140089609668368, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 140089665507376, 140089609474016, 140089665507376, 140089609474016, 140089665507376, 140089609474016, 14
0089665507376, 140089609474016, 140089665507376, 140089609474016, 140089609668368, 140089814550512, 140089665507376, 140089609474016, 140089665507376, 140089609474016, 140089665507376,
 140089609474016, 140089665507376, 140089609474016, 140089665507376, 140089609474016, 140089665507376, 140089609474016, 140089665507376, 140089609474016, 140089665507376, 1400896094740
16, 140089665507376, 140089609474016, 140089665507376]

[140089814745104, 140089814755312, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 140089814755632, 140089814755664, 140089814755696, 140089814755728, 140089814755760, 1400
89814755504, 140089814755792, 140089814755824, 140089814755856, 140089814755888, 140089814755920, 140089814755952, 140089814755984, 45, 28, 11, 11, 1, 0, 0, 0, 0]

9.97949568632597e25

What is wrong with the output? What are you expecting?

I’m expecting the last bit of the output to be [1, 1, 1, 1....], and [13, 13, 13, 13, 13....] but instead I get nonsense: [140089609474016, 140089609668368, 1, 1, 1, 1...]

When you set distances_numbers[number+suit], number is always in the range 2:13 and suit is in 1:4, so you’re only ever modifying distances_numbers[3:17].

The random-looking numbers (like 140089609474016) are the ones that you never set.

2 Likes