// This program illustrates the use of arrays // We use an array (outcomes[]) to first record the // outcomes of simulating a die roll (outcomes [1..6] // Next, we use another array (counts[]) to count how many // of each number were obtained out of a N trials.
void setup() { int N = 1000000; int[] outcomes = new int[N]; // populate the array with dice rolls for (int i=0; i < outcomes.length; i++) { outcomes[i] = int(random(1, 7)); } //println(outcomes); // Next, count the number of times each outcome was obtained // Firstm initialize counts[] int[] counts = new int[7]; for (int i=0; i < counts.length; i++) { counts[i] = 0; } // Then, count for (int i=0; i < outcomes.length; i++) { counts[outcomes[i]]++; } // output results println("Counts..."); println(counts); } // setup()