Sorry for flood but I solved the problem, It was my mistake…
while random_x >= self.last_pos - 200 and random_x <= self.last_pos + 200 do
random_x = m.random(50, 590)
end
This worked fine while self.last_pos is initially 320. Then the random_x value should be between 120 and 520. So thanks for ideas