probability- sum of 2 cubes

Hi guys,

I'm new here and new to Java (and pretty much to programming as a whole :) )

I've started studying Software Engineering, and got HW from the beginnners-JAVA course.

So, I've made the program, and in 1 section of it, I was asked to make the program print the probability of different sums when randomaly throwing cubes.

I've used Monte Carlo for it, and got the following results:

*The results are:*

the probability to get a sum of 1 is: 0

the probability to get a sum of 2 is: 0.028

the probability to get a sum of 3 is: 0.083

the probability to get a sum of 4 is: 0.167

the probability to get a sum of 5 is: 0.278

the probability to get a sum of 6 is: 0.417

Well, somehow I'm not sure i've done it right and those numbers doesn't seem so accurate to me... that's coz the sum of those probabilities covers almost 100% when there are 6 other options with high probabilities (like 7= 3+4, 4+3, 5+2, 2+5 etc...).

I've added that part of the code so that you can see what I've done:

*case '2' : { *

final int trials_number = 10000000;

Integer cube_1 = null;

Integer cube_2 = null;

Integer sum = null;

Integer sum_eq_1 = 0;

Integer sum_eq_2 = 0;

Integer sum_eq_3 = 0;

Integer sum_eq_4 = 0;

Integer sum_eq_5 = 0;

Integer sum_eq_6 = 0;

for (int i = 0 ; i < trials_number; i++) {

cube_1 = (int)(Math.random()*6+1);

cube_2 = (int)(Math.random()*6+1);

sum = (cube_1+cube_2);

switch (sum) {

case 1 : sum_eq_1++;

case 2 : sum_eq_2++;

case 3 : sum_eq_3++;

case 4 : sum_eq_4++;

case 5 : sum_eq_5++;

case 6 : sum_eq_6++;

}

}

DecimalFormat fmt = new DecimalFormat ("0.###");

System.out.println("The results are:");

System.out.println("the probability to get a sum of 1 is: " + fmt.format((double)sum_eq_1/trials_number));

System.out.println("the probability to get a sum of 2 is: " + fmt.format((double)sum_eq_2/trials_number));

System.out.println("the probability to get a sum of 3 is: " + fmt.format((double)sum_eq_3/trials_number));

System.out.println("the probability to get a sum of 4 is: " + fmt.format((double)sum_eq_4/trials_number));

System.out.println("the probability to get a sum of 5 is: " + fmt.format((double)sum_eq_5/trials_number));

System.out.println("the probability to get a sum of 6 is: " + fmt.format((double)sum_eq_6/trials_number) + "\n");

}

break;

Thanks a lot!!

Mapisto.

Re: probability- sum of 2 cubes

Your switch statement only cares for the sums 1 ... 6 and ignores the sums 7 ... 12.

kind regards,

Jos

Re: probability- sum of 2 cubes

Tnx for ur answer,

I was asked to present only the 1-6 sums.

but my question was about the values i've got. they seem strange, don't they ?

Re: probability- sum of 2 cubes

Quote:

Originally Posted by

**Mapisto** but my question was about the values i've got. they seem strange, don't they ?

Not really because in Java (and C and C++ and a few more languages) the case clauses 'fall though'; i.e. when the last statement of a case statement has finished the first statement of the next case clause (if any) executes, unless you add a break statement after each case clause.

kind regards,

Jos

Re: probability- sum of 2 cubes

Oh hell, u're right :|

I forgot the breaks! :|

Tnx man! I believe now it should work fine :)

Re: probability- sum of 2 cubes

I've got another question about this 1.

I've used decimalformat so that i'll have 3 numbers after the point.

In every1 all was fine, except zero. i know it sounds dumb, but i need it to print "0.000". how can i do it?

tnx!

Re: probability- sum of 2 cubes

Quote:

but i need it to print "0.000"

The thing to do in cases like these - where you can't figure out how to get the behaviour you want - is to read the DecimalFormat API docs. The relevant bit seems to be: 0 means digit, # means digit (zero shows as absent).

You **don't** want zero to show as absent (*), so try the other option:

Code:

`DecimalFormat fmt = new DecimalFormat ("0.000"); // try with zero, but also other integral values like 42`

-----

(*) Perhaps it's just me, but I don't find the documentation all that clear in this case. Especially this "show as absent". Wtf: a thing EITHER shows OR it is absent.