In C++
Create a program that rolls two dice. It should roll these dice multiple times to find the probability of having the sum of the rolls add to 7. You should use a loop to roll the dice and use a counter to keep track of how many times they add to seven.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int count = 0, total = 100000; for (int i = 0; i < total; ++i) { if ((1 + rand()%6) + (1 + rand()%6) == 7) { ++count; } } cout << "the probability of having the sum of the rolls add to 7 is " << count/(double)total << endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.