# Solved: How do I display ten random numbers in Java?



## Statler (Mar 5, 2006)

Yes. I am a beginner, and so I was hoping if someone knew how to display *10* random real numbers between 0 and 10 in Java. The problem I am having is displaying 10 different numbers generated from one Math.random statement. This is what I have so far:

public class RandomNumber {

public static void main ( String[] args ) {

double number;

number = Math.random() * 11;

System.out.println ("Here are ten random real numbers between 0 and 10:");

System.out.println (number);
}
}


----------



## Chicon (Jul 29, 2004)

Just try this :


```
[SIZE=2]
Random rnd = new Random();      
double number = rnd.nextDouble() * 11.0;
[/SIZE]
```


----------



## Statler (Mar 5, 2006)

I tried that and I get an error:


```
public class RandomNumber {
    
	public static void main ( String[] args ) {
    	
		Random rnd = new Random();      
		double number = rnd.nextDouble() * 11.0;

		System.out.println (number);
    }
}
```
I just really want to know how to display 10 random numbers from that one single random statement. An example output would be:

5
3
5
2
7
6
7
1
9
5


----------



## Chicon (Jul 29, 2004)

You forgot the import of the Random class :


```
[SIZE=2]
import java.util.Random;

public class RandomNumber {
    
    public static void main(String[] args) {
        Random rnd = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(rnd.nextInt(10) + 1);
        }
    }
}

[/SIZE]
```


----------



## Statler (Mar 5, 2006)

Okay, thanks alot for the help!


----------



## Chicon (Jul 29, 2004)

You're welcome !


----------



## Statler (Mar 5, 2006)

Darn it. One more thing I forgot to say. Can anyone tell me how you would display random real numbers between 20 and 30 because rnd.nextDouble() seems to only work between 0 and another number.


----------



## Chicon (Jul 29, 2004)

Simply this way :


```
[SIZE=2]
Random rnd = new Random();
double val = rnd.nextDouble() * 10.0; [COLOR=Blue]// the command will produce a real number betweem 0 and 10[/COLOR]
                                      [COLOR=Blue]// as rnd.nextDouble() returns a value between 0 and 1[/COLOR]
val = val + 20.0;                     [COLOR=Blue]// val will contain a real number between 20 and 30[/COLOR]
                                      [COLOR=Blue]// also, notice the way to write a real number : a decimal point is mandatory
                                      // to avoid the confusion with an integer[/COLOR]
[/SIZE]
```


----------



## Statler (Mar 5, 2006)

Thanks again.


----------

