I've got some strange results, which i can't explain.

I roll a die for say 100.000.000 times, add all results and divide by the number of the rolls.

This is how i roll a die:

public static int rollDice(){ int result; Random r = new Random(); int randint = r.nextInt(6); result=randint+1; return result; }

This is how i do the calculations:

public static void main(String[] args) { long sum,result, n; double mean; n=100000000; sum =0; result =0; System.out.println(n+" rolls"); for(int i=0;i<n;i++){ result = rollDice(); sum=sum+result; } mean=sum; mean=mean/n; System.out.println("relative frequency: "+mean); }

sample output:

100000000 rolls

relative frequency: 3.49980715

Of course the relative frequency (->mean) should approximate the expected value of a single roll, which is 3.5. To be more precise, after this number of rolls the result should lie within 3.5 +/- 0.001 with a probability of 99,9%.

This seems to work.

But now comes the riddle.

In a separate sum (->vsum) i add only those dices, which don't show a six and still divide by the total number of rolls.

The relative frequency (->vmean) of this modified counting should approximate 2.5, again 2.5 +/- 0.001 with a probability of 99,9%.

I take the original main method, and add some more lines:

public static void main(String[] args) { long sum,result, n,vsum; n=100000000; System.out.println(n+" rolls"); double mean, vmean; sum =0; vsum =0; result =0; for(int i=0;i<n;i++){ result = rollDice(); sum=sum+result; if(result != 6){ vsum = vsum + result; } } mean=sum; mean=mean/n; vmean=vsum; vmean=vmean/n; System.out.println("relative frequency: "+mean); System.out.println("relative frequency, modified counting: " +vmean); }

sample output:

100000000 rolls

relative frequency: 3.50447067

relative frequency, modified counting: 2.51237313

Not only the modified value is way out of bounds, but also the original one is too high!

I've checked several times, the results for the variable mean are constantly too high, but i didn't change anything in the code which should affect this variable.

Right now i'm using widows/eclipse, but i think i had similar results with linux/console (i'll check on this).

Something for the X-files?

Cheers, Rumo