your average noob
your average noob

Reputation: 21

How to average random numbers in java?

thanks in advance for any help I'm in an intro to java class and our home work was to generate 10 random numbers between 1&50 which I got and then average the generated numbers. I can't figure out how to average them here's what I have. Is there a way to store each random number as a variable?

public class randomNumberGen
{
     public static void main(String [] args)
     {
         Random r=new Random();

         for (int i=1;i<=10;i++){
             System.out.println(r.nextInt(50));
             System.out.println();
              int average = (i/4);

              System.out.println("your average is"+average);

         }
     }
}

Upvotes: 1

Views: 6980

Answers (7)

user5055454
user5055454

Reputation: 75

Average means you should add everything up and devide it by the number of elements (50).

import java.util.Random;
class Homework {
    public static final Random RANDOM = Random(); // never regenerate randoms
    public static void main(String args[]) {
        final int N = 50;
        int sum = 0;
        for (int i = 0; i < N; ++i) {
             sum += RANDOM.nextInt(50)+1;
        }
        System.out.println("Avg: "+ sum / (float) N);
    }
}

This should do the trick. Try to learn from it not just C+P.

Ps: Friggin annoying to write code on a phone.

Upvotes: -1

use streams with java 8

 final int numberOfRandom = 10;
 final int min = 0;
 final int max = 50;
 final Random random = new Random();

 System.out.println("The ave is: "+random.ints(min, max).limit(numberOfRandom).average());

Upvotes: 3

Jonatan
Jonatan

Reputation: 1242

Average = Sum of numbers / amount of numbers

int sum = 0;
for (int i=1;i<=10;i++){
    sum += r.nextInt(50) +1; //nextInt 50 produces value 0 to 49 so you add 1 to get 1 to 50 OR as suggested in the comments sum/10d
}
System.out.println("Average is: " + sum/10) // If you want the result in double (with decimals) just write sum*1.0/10

You could also do the same with a while loop.

    int i = 0;
    int sum = 0;
    while(i < 10){
        sum += r.nextInt(50) +1;
        i++;
    }
    System.out.println("Average is: " + sum*1.0/i);

Or even shorter with lambda expressions: (/java 8 streams)

OptionalDouble average = IntStream.range(1, 10).map(x-> x = r.nextInt(50) +1).average();
System.out.println("Average is "+ average.getAsDouble());

.map(x-> x = r.nextInt(50) +1) // maps (changes) each value from 1 to 10 to a random number between 1 and 50
.average(); // calculates the average.

Upvotes: 0

Saiyan
Saiyan

Reputation: 61

Store variables outside of the loop to store both the total amount of numbers generated as well as the sum of those numbers. After the loop completes, divide the sum by the total amount of numbers.

public static void main(String [] args)
{
    Random r=new Random();
    double sum = 0;
    int totalNums;

    for (totalNums=1;totalNums<=10;totalNums++){
        int randomNum = r.nextInt(50);
        sum += randomNum;
        System.out.println(randomNum);
    }

    double average = sum/totalNums;
    System.out.println("your average is: "+average);
}

Upvotes: 0

Rubasace
Rubasace

Reputation: 969

First of all you have to replace "r.nextInt(50)" for "r.nextInt(50) + 1" because r.nextInt(n) returns a number between 0 (inclusive) and n (exclusive). Then, you know that an average is just a sum of n values divided by n. What you can do is just declare a "total" variable initialized to 0 before the loop. On each iteration you add to this variable the random value generated by r.nextInt(50). After the loop you can just divide the total by 10 so you get the average.

PS: it's a good practice to don't use "magic numbers", so it would be perfect (and luckily your teacher will have it in count) if you declare a constant for the number of iterations and then use it both in the loop condition and in the average calculation. Like this, if you have to make it for 100 numbers you only have to change the constant value from 10 to 100 instead of replacing two 10's por two 100's. Also this gives you the chance to give semantic value to these numbers, because now they will be "AMOUNT_OF_NUMBERS = 10" instead of just "10".

Upvotes: 2

QBrute
QBrute

Reputation: 4534

Like every average, it's sum of elements / amount of elements. So let's apply it here:

import java.util.Random;

public class randomNumberGen
{
    public static void main(String [] args)
    {
        Random r=new Random();
        double sum = 0; // is double so to prevent int division later on
        int amount = 10;
        int upperBound = 50;

        for (int i = 0; i < amount; i++){
            int next = r.nextInt(upperBound) + 1; // creates a random int in [1,50]
            System.out.println(next);

            sum += next; // accumulate sum of all random numbers
        }

        System.out.println("Your average is: " + (sum/amount));
    }
}

Upvotes: 0

Kabukki
Kabukki

Reputation: 108

Simply create a variable sum starting at zero that you increment at each iteration. After the loop, simply divide by the number of elements..

Upvotes: -1

Related Questions