(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 5989, 219]*) (*NotebookOutlinePosition[ 6630, 242]*) (* CellTagsIndexPosition[ 6586, 238]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Throwing Dice", "Title", TextAlignment->Center, TextJustification->0, FontSize->18], Cell["In this notebook, we'll simulate the rolling of dice.", "Text"], Cell[CellGroupData[{ Cell["Clear symbols", "Section", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ In order to avoid interference from symbols defined in other \ notebooks, we first Clear and Remove all symbols. We assume that the \ relevant symbols are in the Global` context.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Clear[\"Global`*\"]", "Input", AspectRatioFixed->True], Cell["Remove[\"Global`*\"]", "Input", AspectRatioFixed->True] }, Open ]], Cell[CellGroupData[{ Cell["Load some useful packages", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(<< Statistics`DiscreteDistributions`\)], "Input"], Cell[BoxData[ \(<< Statistics`DataManipulation`\)], "Input"], Cell[BoxData[ \(<< Graphics`Graphics`\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Simulate the dice!", "Section", Evaluatable->False, AspectRatioFixed->True], Cell["Possible outcomes of throwing a single die:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(dots\ = \ {1, 2, 3, 4, 5, 6}\)], "Input"], Cell["\<\ We define a uniform probability distribution for the results 1 to \ 6, and call it \"dice\":\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(dice\ = DiscreteUniformDistribution[6];\)\)], "Input"], Cell[BoxData[ \(\(?DiscreteUniformDistribution\)\)], "Input"], Cell["How many times we toss the die => trials:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(trials\ = \ 120;\)\)], "Input"], Cell["Toss it (suppressing output with a ; when trials is large):", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(toss\ = \ RandomArray[dice, trials];\)\)], "Input"], Cell[BoxData[ \(\(?RandomArray\)\)], "Input"], Cell["\<\ Count how many of each number (between 1/2 and 3/2, 3/2 and 5/2, \ etc):\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(freq\ = \ BinCounts[toss, { .5, 6.5, 1}]\)], "Input"], Cell[BoxData[ \(\(?BinCounts\)\)], "Input"], Cell["Plot the frequency of each result as a histogram:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(BarChart[freq]\)], "Input"], Cell["\<\ Efficient way to add up the elements of the list \"dots\" ==> \ Apply the \"Plus\" operator to each element:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(Plus\ @@ \ dots\)], "Input"], Cell["Calculate the average value of the number of dots:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(N[\ Plus\ @@ \ \((freq*dots)\)\ /\ trials\ ]\)], "Input"], Cell["Calculate the average value of the number of dots squared:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(N[\ Plus\ @@ \ \((freq*dots^2)\)\ /\ trials\ ]\)], "Input"], Cell["Let's define a function that combines all of these steps:", "Text"], Cell[BoxData[ \(Rolldice[ trials_]\ := \ \((\[IndentingNewLine]dots\ = \ {1, 2, 3, 4, 5, 6}; \[IndentingNewLine]dice\ = DiscreteUniformDistribution[6]; \[IndentingNewLine]toss = RandomArray[dice, trials]; \[IndentingNewLine]freq = BinCounts[toss, { .5, 6.5, 1}]; \[IndentingNewLine]BarChart[ freq]; \[IndentingNewLine]avgdots\ = \ N[Plus @@ \((freq*dots)\)/ trials]; \[IndentingNewLine]avgdotssq\ = \ N[Plus @@ \((freq*dots^2)\)/trials]; \[IndentingNewLine]sdev\ = \ Sqrt[avgdotssq - avgdots^2]; \[IndentingNewLine]Print["\", avgdots, "\< standard deviation = \>", sdev])\)\)], "Input"], Cell[BoxData[ \(Rolldice[12]\)], "Input"], Cell[BoxData[ \(Rolldice[120]\)], "Input"], Cell[BoxData[ \(Rolldice[1200]\)], "Input"], Cell[BoxData[ \(Rolldice[12000]\)], "Input"], Cell[BoxData[ \(Rolldice[120000]\)], "Input"], Cell[BoxData[ \(Rolldice[1200000]\)], "Input"] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for X", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{787, 924}, WindowMargins->{{163, Automatic}, {Automatic, 16}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 95, 3, 93, "Title"], Cell[1837, 56, 69, 0, 32, "Text"], Cell[CellGroupData[{ Cell[1931, 60, 80, 2, 60, "Section", Evaluatable->False], Cell[2014, 64, 251, 6, 50, "Text", Evaluatable->False], Cell[2268, 72, 62, 1, 27, "Input"], Cell[2333, 75, 63, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2433, 81, 92, 2, 60, "Section", Evaluatable->False], Cell[2528, 85, 69, 1, 27, "Input"], Cell[2600, 88, 64, 1, 27, "Input"], Cell[2667, 91, 54, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2758, 97, 85, 2, 60, "Section", Evaluatable->False], Cell[2846, 101, 107, 2, 32, "Text", Evaluatable->False], Cell[2956, 105, 62, 1, 27, "Input"], Cell[3021, 108, 164, 5, 32, "Text", Evaluatable->False], Cell[3188, 115, 77, 1, 27, "Input"], Cell[3268, 118, 65, 1, 27, "Input"], Cell[3336, 121, 105, 2, 32, "Text", Evaluatable->False], Cell[3444, 125, 54, 1, 27, "Input"], Cell[3501, 128, 123, 2, 32, "Text", Evaluatable->False], Cell[3627, 132, 74, 1, 27, "Input"], Cell[3704, 135, 49, 1, 27, "Input"], Cell[3756, 138, 144, 5, 32, "Text", Evaluatable->False], Cell[3903, 145, 74, 1, 27, "Input"], Cell[3980, 148, 47, 1, 27, "Input"], Cell[4030, 151, 113, 2, 32, "Text", Evaluatable->False], Cell[4146, 155, 47, 1, 27, "Input"], Cell[4196, 158, 181, 5, 32, "Text", Evaluatable->False], Cell[4380, 165, 49, 1, 27, "Input"], Cell[4432, 168, 114, 2, 32, "Text", Evaluatable->False], Cell[4549, 172, 78, 1, 27, "Input"], Cell[4630, 175, 122, 2, 32, "Text", Evaluatable->False], Cell[4755, 179, 80, 1, 27, "Input"], Cell[4838, 182, 73, 0, 32, "Text"], Cell[4914, 184, 744, 13, 171, "Input"], Cell[5661, 199, 45, 1, 27, "Input"], Cell[5709, 202, 46, 1, 27, "Input"], Cell[5758, 205, 47, 1, 27, "Input"], Cell[5808, 208, 48, 1, 27, "Input"], Cell[5859, 211, 49, 1, 27, "Input"], Cell[5911, 214, 50, 1, 27, "Input"] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)