2 Replies - 1631 Views - Last Post: 08 October 2012 - 11:36 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • 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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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

Posted 08 October 2012 - 11:36 PM

View PostBrovahkiin501, 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