Results 1 to 7 of 7
Thread: probability sum of 2 cubes
 12012011, 04:40 PM #1Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
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 beginnnersJAVA 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.
 12012011, 04:45 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,523
 Blog Entries
 7
 Rep Power
 20
Re: probability sum of 2 cubes
Your switch statement only cares for the sums 1 ... 6 and ignores the sums 7 ... 12.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 12012011, 04:57 PM #3Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
Re: probability sum of 2 cubes
Tnx for ur answer,
I was asked to present only the 16 sums.
but my question was about the values i've got. they seem strange, don't they ?
 12012011, 05:32 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,523
 Blog Entries
 7
 Rep Power
 20
Re: probability sum of 2 cubes
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,
Joscenosillicaphobia: the fear for an empty beer glass
 12012011, 05:46 PM #5Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
Re: probability sum of 2 cubes
Oh hell, u're right :
I forgot the breaks! :
Tnx man! I believe now it should work fine :)
 12012011, 06:04 PM #6Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
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!
 12012011, 10:28 PM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Re: probability sum of 2 cubes
but i need it to print "0.000"
You don't want zero to show as absent (*), so try the other option:
Java 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.
Similar Threads

How to generate bigram/trigram/unigram probability?
By findjoe in forum LuceneReplies: 4Last Post: 10042010, 08:01 AM 
Calculating Squares and Cubes in a table
By aldorfski_17 in forum New To JavaReplies: 4Last Post: 03222010, 07:17 PM
Bookmarks