-5

Hi can anyone help me with my code below. I am trying to implement a random number into the iterator below it, where the number '100' would be a random number between 1 and 100.

any help would be great thanks

Daniel

Random randomGenerator = new Random();
    for (int idx = 1; idx <= 100; ++idx){
      int randomInt = randomGenerator.nextInt(100);

    }

      Iterator<Rectangle> iter = clouds.iterator();
      while(iter.hasNext()) {
         Rectangle cloud = iter.next();
        //this effects the speed of downward movement
         cloud.x -= 100 * Gdx.graphics.getDeltaTime();
         if(cloud.x + 80 < 0) iter.remove();

      }
AurA
  • 12,135
  • 7
  • 46
  • 63
Djgriff
  • 15
  • 3
  • 1
    -1. Possibly a duplicate. http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range. Also check relevant API docs. http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt%28int%29 – verisimilitude Jun 12 '12 at 08:13
  • Iteration is iteration. It iterates over everything. If you only want to remove a random element, do that directly: iteration is a complete waste of time. – user207421 Jun 12 '12 at 10:57

1 Answers1

0

That's pretty trivial.

Random r = new Random();

Iterator<Rectangle> iter = clouds.iterator();

while(iter.hasNext()) {
  Rectangle cloud = iter.next();
  //this effects the speed of downward movement
  cloud.x -= (r.nextInt(100) + 1) * Gdx.graphics.getDeltaTime();

  if(cloud.x + 80 < 0) iter.remove();
}

See the documentation for java.util.Random.

Kallja
  • 5,362
  • 3
  • 23
  • 33