
Patrick B. answered 02/12/20
Math and computer tutor/teacher
They will all be random except for the last...
Here's an algorithm:
Mean = total/count
mean * count = total
So you know in advance what the total of the N statistics must be.....
So then you calculate S, the running total of N-1 random statistics
The Nth statistic must be total - S...
here some working code..........
public static void main( String args[])
{
int mean,N;
mean=N=0;
if (args.length>1)
{
mean = Integer.parseInt(args[0]);
N = Integer.parseInt(args[1]);
}
else
{
System.out.println(" Please input the mean and the count on the command line ");
return;
}
int randNum=0;
long total = mean*N;
long running_total = 0;
int [] randStats = new int[N];
for (int iLoop=0; iLoop<N-1; iLoop++)
{
randNum = (int) (Math.random()*999 +1);
System.out.println(" random # " + randNum);
randStats[iLoop]=randNum;
running_total += randNum;
}
System.out.println(" Running total is " + running_total);
randStats[N-1] = (int) (total - running_total);
System.out.println(" random # " + randStats[N-1]);
long sum=0;
for (int iLoop=0; iLoop<N; iLoop++)
{
sum+=randStats[iLoop];
}
System.out.println(" mean verified : "+ sum/N);
}
}