title: java.lang.IllegalArgumentException: n must be positive Android Studio
body: <p>I'm working on a Asteroid dodger game in Android Studio. I create random positions for each of the asteroids using the in build <code>random</code> generator in java. I keep getting <code>java.lang.IllegalArgumentException: n must be positive</code>, however it still runs fine on my phone.</p>
  
  <p>Exception:</p>
  
  <pre><code>java.lang.IllegalArgumentException: n must be positive
      at java.util.Random.nextInt(Random.java:300)
      at ca.alnitakstudios.Asteroids.Asteroids.&lt;init&gt;(Asteroids.java:38)
      at ca.alnitakstudios.Screens.GameScreen.&lt;init&gt;(GameScreen.java:36)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
      at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:381)
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>public class Asteroids {
  
  private Random random;
  
  private int amountOfAsteroids, fastestSpeed, slowestSpeed,
  biggest, smallest;
  private int[] x, y, speeds, radii;
  
  public Asteroids() {
      amountOfAsteroids = 5;
      x = new int[amountOfAsteroids];
      y = new int[amountOfAsteroids];
  
      speeds = new int[amountOfAsteroids];
      radii = new int[amountOfAsteroids];
  
      fastestSpeed = 7;
      slowestSpeed = 3;
  
      biggest = 20;
      smallest = 10;
  
      random = new Random();
  
      for (int i = 0; i &lt; amountOfAsteroids; i++) {
          //init x &amp; y placement
          x[i] = random.nextInt(GameScreen.width * 2) + GameScreen.width;
          y[i] = random.nextInt(GameScreen.height);
  
          //Setting speeds
          speeds[i] = random.nextInt(fastestSpeed - slowestSpeed) + slowestSpeed;
  
          //Setting the radii
          radii[i] = random.nextInt(biggest - smallest) + smallest;
      }
  }
  
  public void draw(Canvas canvas) {
      Paint paint = new Paint();
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.GRAY);
  
      for(int i = 0; i &lt; amountOfAsteroids; i++) {
          x[i] -= speeds[i];
          canvas.drawCircle(x[i], y[i], radii[i], paint);
      }
  
      checkIfOutOfBounds();
  }
  
  public void checkIfOutOfBounds() {
      for(int i = 0; i &lt; amountOfAsteroids; i++) {
          if(x[i] &lt; 0) {
              //x &amp; y placement after its been out of bounds
              x[i] = random.nextInt(GameScreen.width * 2) + GameScreen.width;
              y[i] = random.nextInt(GameScreen.height);
  
              //Setting new speeds
              speeds[i] = random.nextInt(fastestSpeed - slowestSpeed) + slowestSpeed;
              //Setting new radii
              radii[i] = random.nextInt(biggest - smallest) + smallest;
          }
      }
  }
  </code></pre>
  
  <p>}</p>