Usually, if adding something random in a random place seems to solve an issue, you have a memory problem.
Low on memory can cause this, another culprit is writing outside the boundaries of an array.
Can't view your code, the colour scheme is poorly chosen.