Answer to Question #133918 in C for Mo

Question #133918
How can I write a C program to shoot a two dices 1000 times and print the numbers you make, then calculate the average number, and finally print the average number out
1
Expert's answer
2020-09-19T16:34:09-0400

The code for above c program can be given by,




public static void main( String[] args )

{

int dice1;

int dice2;

// The amount of tests

final int SIZE = 10000000;




// store all results we got from a single test

int[] result = new int[SIZE];




// loop through the tests

for(int i = 0; i < SIZE;i++)

{

// initialize counter for every test

int rolls = 0;

do

{

// roll counter increases

rolls++;

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

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

// check if condition is met.

}while(dice1 != 1 || dice2 != 1);

// store the result of the test

result[i] = rolls;

}

// calculate the average amount of rolls necessary

double avg = Arrays.stream( result ).sum() / (double)SIZE;

System.out.println( avg );

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS