Name: _________________________________________


CSC 121: Computers and Scientific Thinking
Spring 2020

Lab 4: Slots Simulations

One of the ways that computers have transformed science and mathematics is through software simulations. A model of a complex process, such as the spread of a disease or climate change, can be developed and then studied to gain insights into that process that cannot easily be duplicated. For example, an epidemiologist studying the spread of a disease can change parameters in the model (e.g., rate of infection, degree of social distancing) and study how policy changes might impact the spread. In addition, software models can simulate extended periods of time very quickly, such as the impact of climate change over decades or centuries.

In this lab, you will apply these methods to studying the expected results of a game of chance. In Unit 10, you designed and developed a Web page that simulated a slot machine. Each time the player clicked on a button, the slot images changed and wins/losses were maintained. While this page was sufficient for playing an interactive game, if we wanted to analyze the odds of winning or losing in slots we would need a page that could simulate many, many games at once.




Average Losses

The Web page slotsSim.html is provided and can be used to simulate repeated slots games. The page contains two text boxes, in which the user can specify the starting number of credits and the number of spins for each game. At the click of the button, the page simulates 100,000 games and displays the average number of credits lost per game (and the percentage of the initial amount that loss represents). For example, the message:

Average number of credits lost: 6.5 out of 20 (32.5%)

would mean that over the 100,000 games, an average of 6.5 credits were lost. That average of 6.5 represents a 32.5% loss from the initial 20 credits.


EXERCISE 1:    The default values in the text boxes specify games of 40 spins, each starting with 20 credits. Use the page to perform five different simulations using the default values and report the statistics for each below. How consistent were the results?




EXERCISE 2:    Fill in the following table by performing simulations with the slotsSim.html page. For each pair of simulation parameters (initial credits and number of spins), perform five different simulations and enter the average percentage lost for each simulation:
initial credits# of spinssim1 loss % sim2 loss %sim3 loss %sim4 loss %sim5 loss %
2040     
2080     
20160     
4080     
40160     
40320     
80160     
80320     
80640     


EXERCISE 3:    Based on your data from EXERCISE 2, answer the following questions, citing data to justify your conclusions:




Customizing the Number of Games

As it is currently written, the slotsSim.html page always simulates 100,000 games and displays the average loss over those games. There are times when that number might be overkill, and others when it might not be enough. As we have learned throughout this class, simulations involving randomness can be skewed if the number of experiments is small (e.g., it would not be surprising of 7 out of 10 coin flips turned up heads, but it would be if 7,100 out of 10,000 did). If we wanted precise and consistent slots statistics, it is possible that 100,000 games might not be sufficient.


EXERCISE 4:    Download a copy of slotsSim.html and save it on your computer under the same YOURNAME-Sim.html, where YOURNAME is your last name (e.g., Reed-Sim.html). Then, modify the page so that it has an additional text box, in which the user can specify the number of games to simulate. Modify the SimulateGames function so that it accesses this text box, simulates that many games (in place of the default 100,000), and displays the correct statistics. You should also update the text that appears at the top of the Web page to match the new design.

Once you have completed your modifications, repeat the simulations from EXERCISE 2, only now over 1,000,000 games.

initial credits# of spinssim1 loss % sim2 loss %sim3 loss %sim4 loss %sim5 loss %
2040     
2080     
20160     
4080     
40160     
40320     
80160     
80320     
80640     






EXERCISE 5:    Based on your data from EXERCISE 4, answer the following questions, citing data to justify your conclusions:




Counting the number of "wins"

Currently, your slots simulation page calculates and displays the average credit loss per game for a player. This is an effective statistics for discouraging gambling, as it shows how the game (as we defined it) will result in long-term losses by the player. However, some recreational gamblers look at games of chance differently. When viewed as a hobby to pass the time, a gambler might see small losses as the cost of their entertainment. What might be of more interest to them is likelihood of "winning" or losing" a session of games. That is, how often a game session end with more or fewer credits than the player started with? It might be argued that if they win more often than they lose, the feeling of satisfaction at the end of those winning days would be worth the overall loss of credits.


EXERCISE 6:    Modify your slots page so that, in addition to the average loss per game, the page also keeps track of and displays the number of losing games. We will define a losing game to be one in which the player ends with fewer credits than they started with. This will involve adding a test inside the loop in SimulateGames to see if the number of credits lost by each game is > 0. (Note: in the case of a winning game, the creditsLostThisGame variable would be negative.) If the game results in a loss, a counter should be incremented. The final number and percentage of losing games should be displayed along with the average loss. For example: Average number of credits lost: 6.23058 out of 20 (31.2%) Number of losing games: 77128 out of 100000 (77.1%)

Once you have completed your modifications, repeat the simulations from EXERCISE 4, and report the percentages of losing games.

initial credits# of spinssim1 losing % sim2 losing %sim3 losing %sim4 losing %sim5 losing %
2040     
2080     
20160     
4080     
40160     
40320     
80160     
80320     
80640     


EXERCISE 7:    Based on your data from EXERCISE 6, answer the following questions, citing data to justify your conclusions:


Submit via Blueline your modified slots simulation page (YOURNAME-Sim.html) along with the written answers to the exercises.