The random module can generate random numbers. One application is rolling a fair
ID: 3839651 • Letter: T
Question
The random module can generate random numbers. One application is rolling a fair dice, which will produce a 1, 2, 3, 4, 5 or 6. The code below should explain how to generate random ints: > > > import random > > > for i in range (20): n = random.randint(1, 6) print (n, end ='') 4 4 1 3 1 3 2 5 3 1 4 5 4 5 6 1 3 3 2 4 > > > write a function, roll_dice(n), which returns the result of rolling n fair dice in a list. n is an int and is > =1. Sample run (your results will almost certainly be different from mine after a we are simulating dice rolls): for n in range (1, 7): print (roll_dice(n)) > > > [6] [6, 5] [2, 3, 6] [1, 6, 4, 3] [6, 3, 3, 1, 3] [1, 5, 2, 4, 1, 4] > > >Explanation / Answer
Understanding your problem statement I am writing the code in c++
Please find the below-working code and the output
**********************
code
// C++ program to find number of ways to get sum 'x' with 'n'
// dice where every dice has 'm' faces
#include <iostream>
#include <string.h>
using namespace std;
// The main function that returns number of ways to get sum 'x'
// with 'n' dice and 'm' with m faces.
int findWays(int m, int n, int x)
{
// Create a table to store results of subproblems. One extra
// row and column are used for simpilicity (Number of dice
// is directly used as row index and sum is directly used
// as column index). The entries in 0th row and 0th column
// are never used.
int table[n + 1][x + 1];
memset(table, 0, sizeof(table)); // Initialize all entries as 0
// Table entries for only one dice
for (int j = 1; j <= m && j <= x; j++)
table[1][j] = 1;
// Fill rest of the entries in table using recursive relation
// i: number of dice, j: sum
for (int i = 2; i <= n; i++)
for (int j = 1; j <= x; j++)
for (int k = 1; k <= m && k < j; k++)
table[i][j] += table[i-1][j-k];
/* Uncomment these lines to see content of table
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= x; j++)
cout << table[i][j] << " ";
cout << endl;
} */
return table[n][x];
}
// Driver program to test above functions
int main()
{
cout << findWays(4, 2, 1) << endl;
cout << findWays(2, 2, 3) << endl;
cout << findWays(6, 3, 8) << endl;
cout << findWays(4, 2, 5) << endl;
cout << findWays(4, 3, 5) << endl;
return 0;
}
******************
Output:
****************************
Second Way:
Kindly find the below main logic for the above problem statement
*********************
code
***********************
The above code works perfectly rolling 20 times
P.S : KINDLY FIND THE ABOVE TWO WAYS AND CHOOSE ONE YOU FEEL EASY
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.