<p>Okay, I’m trying to simulate the probability of the sum of two 11 sided die. I have this so far:</p>
<p>import java.util.Scanner;
public class Dice {</p>
<pre><code>public static void main(String args) {
Scanner in = new Scanner(System.in); //input
System.out.print("Number of Rolls: ");
int limit = in.nextInt();
int rolls = 0;
int dice1 = 0;
int dice2 = 0;
int sum = 0;
while(rolls < limit)
{
for(int i = 0; i < 2; i++)
{
dice1 = (int)(Math.random()*11)+1;
dice2 = (int)(Math.random()*11)+1;
sum = dice1 + dice2;
}
rolls++;
}
}
</code></pre>
<p>}</p>
<p>Now, the problem is I can’t figure out how to record the sums so I can use them to find the probability. The only thing i could think of was make a huge if - else if chain that goes something like this…</p>
<p>if (sum == 2)
sum2++
else if (sum ==3)
sum3++
…
else if (sum == 22)
sum22++</p>
<p>I know there’s a much more elegant and shorter solution. What would you do? (MIT, this means you)</p>
<p>I’m embarrassed to have even read it.</p>
<p>DAMMIT Oscar where are you? The world needs you!</p>
<p>Lawl…</p>
<p>Eh, it’s been a while since I used Java/been in Comp Sci. Not sure if it’ll work, but worth a shot.</p>
<p>Use an Array. Like anArray[sum] += 1;, sum being your resulting value of course.</p>
<p>Use an array counter.
The counter array has as many elements as there is possible values, so… 2-22.
Also… I suggest you use an array to store the values of the roll…</p>
<p>IE…
// This is a simulated roll array of 5 single die 6-sided rolls
final int FACES =6;
int rollarray = {4, 4, 3, 6, 2};
int counter = new int[6];</p>
<p>for(int i =0; i < rollarray.length; i++)
{
int roll = roll die…
counter[roll-1] = roll;
}</p>
<p>Ignore the above.</p>
<p>Wait…record the sums of what?</p>
<p>I have to take the sum of the two 11-sided dice across multiple trials (the limit variable) and ultimately divide the frequency of each sum by the number of trials to get the probability of each sum.</p>
<p>So… if I have this right…</p>
<p>We have die 1 and die 2…</p>
<p>And we roll them 5 times.</p>
<p>Yeah, you’re just going to use an array counter. The counter counts as a frequency thing, and the sum of all the elements within the counter array should equal the number of trials you run. So if you want to know the probability of a specific sum, find that sum in the counter array, and divide by the amount of trials.</p>
<p>Btw, don’t be embarrassed to ask. It’s a good question. We all need help at times.</p>
<p>Shouldn’t that be the modulus operator instead of multiplication? What’s the for loop for?</p>
<p>For Math.random? No. That returns a value between 0.0 and 1.0, so you want to multiply it by 11.</p>
<p>By the way, using the method I entailed only has you using 1 for loop to traverse the main array.</p>
<p>Lawl, Java fail for me.</p>
<p>That’s what I was thinking too.</p>
<p>lol, I love this language, even if it’s meant for more Web-based stuff.</p>
<p>Thanks MIT. I ended up using an array but I had to keep it nested because that’s what the assignment is actually supposed to be about (we haven’t even heard the word array in this course yet.) Here’s what I ended up using. Thanks for the help guys :)</p>
<p>import java.util.Scanner;
public class Dice {</p>
<pre><code>public static void main(String args) {
Scanner in = new Scanner(System.in); //input
System.out.print("Number of Rolls: ");
int limit = in.nextInt();
int rolls = 0;
int dice1 = 0;
int dice2 = 0;
int sum = 0;
int[] sumFrequency = new int[23]; //anArray what?
while(rolls < limit) //getting loopy
{
for(int i = 0; i < 2; i++) //getting nested loopy
{
dice1 = (int)(Math.random()*11)+1;
dice2 = (int)(Math.random()*11)+1;
sum = dice1 + dice2;
sumFrequency[sum]++; //anArray again???
}
rolls++;
}
System.out.println("Sum of Dice Probability" ); //output WITH LOOPS
for(int x = 2; x <= 22; x++)
{
System.out.println(x + "'s:" + " " + (double)sumFrequency[x]/limit*100.0 + "%");
}
}
</code></pre>
<p>}</p>