fixed a bug in random loop