if (random > 0.5) { direction = 'right'; }
1 is not included in Math.Random, so it should be < 0.5 == left, >= 0.5 == right.