Random variables changing for no reason

The compiler can do many optimization. For example, the compiler can do pre-calculations to reduce the calculations that are done during runtime. A variable might not be something with a memory location.
If you change something very little in the code, then the compiler might decide to do other optimizations. As a result, a bug might show itself in a different way, or perhaps not at all.

This is actually how a common memory problem behaves (assuming it is a memory problem). Perhaps you ran out of SRAM, or write beyond the limits of an array, or a pointer is used in the wrong way. Enable the maximum warning level of the compiler, that might help.

May we see the complete sketch ?
Which Arduino board do you use ?

1 Like