# Java code generate 10000 random integers; sum, max, min, and average

Page 1 of 1

## 2 Replies - 3871 Views - Last Post: 08 October 2012 - 11:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294837&amp;s=fd40614c2ef2c7e5fa594d7a7bda3e97&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brovahkiin501

• New D.I.C Head

Reputation: 0
• Posts: 48
• Joined: 01-October 12

# Java code generate 10000 random integers; sum, max, min, and average

Posted 08 October 2012 - 11:25 PM

Hi everyone!

I have to create a program which generates 10000 random numbers between 0 and 1 using the Math.random(). We then have to display the largest and smallest numbers, and the average of all the numbers. We are supposed to use a loop. Here is the code I have so far. All I've figured out how to do is generate the random numbers. If somebody could help me from here it would be much appreciated!

```
public class RandAnalysis {
public static void main(String[] args) {
for(int i = 0; i < 10000; i++)
System.out.println("Random number ["+ (i+1) +"]:" + Math.random());

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Java code generate 10000 random integers; sum, max, min, and average

### #2 jdavi134

• D.I.C Head

Reputation: 42
• Posts: 225
• Joined: 26-October 11

## Re: Java code generate 10000 random integers; sum, max, min, and average

Posted 08 October 2012 - 11:36 PM

Variable for highest, variable for lowest, if statements to set the highest and lowest.

Hope that helps.
Was This Post Helpful? 0

### #3 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Java code generate 10000 random integers; sum, max, min, and average

Posted 08 October 2012 - 11:36 PM

Brovahkiin501, on 09 October 2012 - 11:55 AM, said:

Hi everyone!

I have to create a program which generates 10000 random numbers between 0 and 1 using the Math.random(). We then have to display the largest and smallest numbers, and the average of all the numbers. We are supposed to use a loop. Here is the code I have so far. All I've figured out how to do is generate the random numbers. If somebody could help me from here it would be much appreciated!

```
public class RandAnalysis {
public static void main(String[] args) {
for(int i = 0; i < 10000; i++)
System.out.println("Random number ["+ (i+1) +"]:" + Math.random());

}

}

```

Well, about your print function, I feel that the (i+1) is not necessary there as you are already using a loop which increments the i value. You could probably change that to i and in your loop, you could begin from i=1 and i<=10000, so that you get exactly 10000 numbers.

Now, coming to your question of largest and smallest, I shall give you the logic of how to go about it. Say, there are 10 numbers. You need to compare the first number with the other 9 numbers and check to find out if it is the biggest number(or smallest number). Think of a pseudocode for this to happen. If you have any difficulties, we shall help you.

Finally, the average of a series of numbers is the sum of all the numbers divided by the total number of numbers( in this case 10000). Again, try to write a code for this and ask for help if you have any difficulty.I am sure you can do it.

regards,
Raghav
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}