As you can see my code what it does is generate a run of 3 random numbers, for that I use a cycle that is counting K, I am saving those numbers in an array to later add them and get the average, but it happens that I need to do it for each K, that is, when k = 1 prints 1 random result, it adds it and generates the average, and so for 2, 3, 4 until it reaches k = 30.
How could I do this? I have already tried some things and none of them has happened to me.
Code:
public static void main(String[] args) {
double x; //aleatoria
double promedio=0;
double total =0;
for(int k=1; k<31; k++){
x = 0+Math.random()*1;
double arr[] = {x};
System.out.println(x);
for(int contador=0;contador<arr.length;contador++){
total+=arr[contador];
}
promedio = total/30;
}
System.out.println("El total es: "+total);
System.out.println("El promedio es: "+ promedio);
}
}