java - Adding Two Dice 36 Million Times -
the question: write programme rolls 2 dice , adds sum 36,000,000 times , prints how many times each sum calculated.
so need rand 6 numbers twice , add together them - in loop 36 1000000 times, , frequency counter many times each sum found (which ranges 2 12).
now given fact i'm not experienced in java, ran couple of problems. code i've got far:
package twodice; import java.util.random; public class twodice { public static void main(string[] args) { int sum; random randomnumbers = new random(); int[] frequency = new int[13]; (int roll = 2; roll <= 36000000; roll++) { ++frequency[(1 + randomnumbers.nextint(6)) + (1 + randomnumbers.nextint(6))]; } system.out.printf("%s%10s\n", "face", "frequency"); for(int face = 1; face < frequency.length; face++) { system.out.printf("%4d%10d\n", face, frequency[face]); } } }
output:
run: face frequency 1 6001537 2 6003025 3 5997753 4 5997647 5 6000769 6 5999269 7 0 8 0 9 0 10 0 build successful (total time: 0 seconds)
the problems that: 1. sums displaying not 2-12, they're 1-10 (the right number of sums, not right sums... 2. frequencies beingness found 1-6, not 1-6 + 1-6.
thanks of help!
edit: solved oscar lopez! much man!
for first problem, array has wrong size, should prepare it:
int[] frequency = new int[13];
also values @ indexes 0
, 1
0
, loop should start @ face = 2
. sec problem, programme should simulate throwing two dice, not 1 is. seek this:
++frequency[(1 + randomnumbers.nextint(6)) + (1 + randomnumbers.nextint(6))];
java dice
No comments:
Post a Comment