# Adding random number to array list

• 11-13-2012, 03:47 AM
Michael305rodri
Adding random number to array list
I don't understand the list.add(r.nextInt(90) + 10); part. Can someone explain this to me? I recall my professor saying that it has to be minus 1. Basically, 90 + 10 = 100 and 100 -1 = 99 and that's the largest number.
Code:

```//        File:  ArrayListIntro.java  // Uses an "ArrayList of Integer" object to show list processing basics: // using a for statement to traverse a list, and ArrayList methods add(), // size(), and get(). import java.util.ArrayList; import java.util.Random; /**  * A class to demonstrate the basics of list processing  */ public class ArrayListIntro {     public static void main(String[] args) {         Random r = new Random();         ArrayList<Integer> list = new ArrayList<Integer>();                 // ***** POPULATE the list *****                 // add 10 random ints between 10 and 99 to the list         for (int i = 1; i <= 10; i++) {             list.add(r.nextInt(90) + 10);         }```
• 11-13-2012, 10:50 AM
Tolls
Re: Adding random number to array list
nextInt() API.
That's probably a bit too much to read, but the first sentence is the important part:
"
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)...
"
So nextInt(10) will provide a number from 0-9, not 0-10.

In your case nextInt(90) will give you a number from 0-89.
So adding 10 to that gives you 10-99.