Question

(1 point) Using an if Statement In a Loop A common thing to do is to...

(1 point) Using an if Statement In a Loop

A common thing to do is to use variables to keep track of some sort of count. When used in a loop we count things very quickly.

Scenario: If you roll a pair of dice, rolling a 12 (two sixes) is rare. How rare? If you were to roll a pair of dice 1,000 times, on average, how many times would it come up as 12?

To figure this out, we could write code to run an experiment. It would go something like this:

  • Make a loop that simulates rolling a pair of dice 1,000 times.
  • Inside the loop, add an if statement: if die1 + die2 == 12, then add 1 to a counter.
  • After the loop, display the result.

var die1 = -1;

var die2 = -1;

var loopNum = 0; //use to count number of loops

var twelveCount = 0; //use to count 12's

while(loopNum < 1000){

loopNum++;

die1 = randomNumber(1, 6);

die2 = randomNumber(1, 6);

console.log("Rolled a " + die1 + " and a " + die2 + " for a total of " + (die1 + die2));

}

console.log("The number of times 12 was rolled was: " + twelveCount);

console.log("Done.");

The starter code sets up the whole experiment for you, except it doesn't count the number of 12's rolled - that's your job. Note: If you remove (or comment out) the console.log statement that displays every roll of the dice, the experiment will speed up A LOT! You could do tens of thousands of dice rolls in a matter of seconds.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

var die1 = -1;
var die2 = -1;
var loopNum = 0; //use to count number of loops
var twelveCount = 0; //use to count 12's
while(loopNum < 1000){
loopNum++;
die1 = randomNumber(1, 6);
die2 = randomNumber(1, 6);

if ((die1 + die2)==12) {
++twelveCount;
}

}
console.log("The number of times 12 was rolled was: " + twelveCount);
console.log("Done.");


=========================
SEE OUTPUT


Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
(1 point) Using an if Statement In a Loop A common thing to do is to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • With the use of python IDLE version 3.7.2, use the while loop that iterates 100,000 times....

    With the use of python IDLE version 3.7.2, use the while loop that iterates 100,000 times. In the loop code, roll a pair of dice by generating(and summing) two random numbers in the range 1 through 6. Report to the user how many data points have been generated (i.e how many times the dice have been rolled), as in: successfully simulated 100,000 dice rolls. Create a histogram of the dice rolls showing how many times a 2 was rolled, how...

  • Using the previous tutorial, address the following: Imagine an experiment where three dice are tossed and...

    Using the previous tutorial, address the following: Imagine an experiment where three dice are tossed and the number on each die is recorded under Die1, Die2, and Die3. Answer the following questions about the sum of the three numbers recorded from: Die1+Die2+Die3. (Discussions allowed) Hint: Simulate this experiment 1000 times. (Use the same procedure as in the above tutorial, but for the Number of Variables, instead of 1 put 3, since we are rolling 3 dice not one). Next, create...

  • Required in C++ I'm asked to: Print a histogram in which the total number of times...

    Required in C++ I'm asked to: Print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times. Below is my current code. I am not allowed to use arrays or anything too advanced as I just started the class. I would really appreciate the help as I've been stuck on it for a while now. I can only get it to print...

  • Using the previous tutorial, address the following: Imagine an experiment where three dice are tossed and...

    Using the previous tutorial, address the following: Imagine an experiment where three dice are tossed and the numbers on each die is recorded under Die1, Die2 and Die3. Answer the following questions about the sum of the three numbers recorded from: Die1+Die2+Die3. (Discussions allowed) Hint: Simulate this experiment 1000 times. (use the same procedure as in the above tutorial, but for the Number of Variables, instead of 1 put 3; since we are rolling 3 dice not one). Next, create...

  • Write a program that calls the random number generator two times to simulate rolling a pair...

    Write a program that calls the random number generator two times to simulate rolling a pair of dice. It should store the two numbers into variables. It will then determine if the dice roll is a winner (doubles OR add up to 7). Remember to add two includes and also to seed the random number generator to the clock. This will roll two dice and add them together. Doubles, or a total of 7 - YOU ARE A WINNER!!! First...

  • need help with dice excercise For example, if we were writing a calendar program of some...

    need help with dice excercise For example, if we were writing a calendar program of some sort, we might very well use an array with 366 spots to hold the days, not worrying about adding more days to the year. Exercise 23 (C level] For another example, suppose we want to simulate rolling two six-sided dice repeatedly, tallying how many times each total is rolled. We need a memory cell to count the number of times each of the possible...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT