-3
import java.util.Random;
public class exam1
{
    public static void main (String args[])
    {
    //  Scanner scan = new Scanner(System.in);
        int array [] = new int[10];
        Random rand = new Random(1001);
        int sum = 0;
        int sum1 = 0;

        for(int i=0; i<array.length; i++)
        {
            array[i]=rand;
            sum = sum+array[i];
            sum1=sum/array.length;
        }
            System.out.println(sum);
    }
}

I can't get to generate random numbers at all. How to generate 1000 random numbers from size 10 array and find the average of the numbers and print them on the screen?

Cœur
  • 37,241
  • 25
  • 195
  • 267
brazil
  • 11
  • 4

2 Answers2

0

Try this:

    import java.util.Random;
    public class exam1
    {
    public static void main (String args[])
    {
    //  Scanner scan = new    Scanner(System.in);
        int array [] = new int[10];
        Random rand = new Random();
        //You could use just one variable
        int sum = 0;

        for(int i=0; i<array.length; i++)
        {
    //Generate random integer between 0 and 1000
            array[i]=rand.next(1000);
            sum = sum+array[i];
            sum=sum/array.length;
        }
            System.out.println(sum);
    }

}
John
  • 410
  • 1
  • 4
  • 19
0
 import java.util.Random;
public class exam1
{
    public static void main (String args[])
    {
    //  Scanner scan = new Scanner(System.in);
        int array [] = new int[10];
        Random rand = new Random();
        int sum = 0;
        int sum1 = 0;

        for(int i=0; i<array.length; i++)
        {
            array[i]=rand.nextInt(1001);
            sum = sum+array[i];
            sum1=sum/array.length;
        }
            System.out.println(sum1);
    }

}

this works i think

brazil
  • 11
  • 4