title: How can I make an object move randomly
body: <p>I'm making a game where I spawn enemies at the top of the screen, they move downwards to the bottom of the screen. I want to make them move randomdly.</p>
  
  <pre><code>public void move_randomly(){
  
      int random_x_pos = rd.nextInt(700);
  
      if (xPos &lt; random_x_pos){
          xPos += get_speed();
          if (xPos &gt;= random_x_pos){
             // get a new x position
          }
      }
      if (xPos &gt; random_x_pos){
          xPos -= get_speed();
          if(xPos &gt;= random_x_pos){
              // get a new x position
          }
      }
  
      yPos += get_speed();  //makes the object keep moving down
  }
  </code></pre>
  
  <p>The problem is that this method has to be called inside an infinite loop, the one that is constantly repainting the screen, so <em>the random_x_position</em> variable is always changing. I want the random X position given to be "saved" until the object has reached that position and then change it.</p>