(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 4.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. 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[ 10786, 290]*) (*NotebookOutlinePosition[ 11513, 315]*) (* CellTagsIndexPosition[ 11469, 311]*) (*WindowFrame->Normal*) Notebook[{ Cell["A (very) simple pseudo-random number generator", "Text", FontSize->18], Cell[BoxData[{ \(\(x = 0.45;\)\), "\[IndentingNewLine]", \(\(deciles = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\)\), "\[IndentingNewLine]", \(\(n = 15000;\)\), "\[IndentingNewLine]", \(\(xlist = Table[0, {15000}];\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]x = \((Pi + x)\)^5 - Floor[\((Pi + x)\)^5]; \[IndentingNewLine]xlist[\([i]\)] = x; \[IndentingNewLine]y = Ceiling[x*10.0]; \[IndentingNewLine]deciles[\([y]\)] = deciles[\([y]\)] + 1, \[IndentingNewLine]{i, 1, n}];\)\), "\[IndentingNewLine]", \(N[deciles/n]\)}], "Input"], Cell[BoxData[ \(ListPlot[xlist]\)], "Input"], Cell[BoxData[ \(<< Graphics`Graphics`\)], "Input"], Cell[BoxData[ \(BarChart[deciles/n]\)], "Input"], Cell["A random walk simulator", "Text", FontSize->18], Cell[BoxData[{ \(\(nsteps = 7200;\)\), "\[IndentingNewLine]", \(\(poslist = Table[0, {i, 1, nsteps + 1}];\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]x = Random[]; \[IndentingNewLine]If[x < 0.5, poslist[\([i + 1]\)] = poslist[\([i]\)] + 1, poslist[\([i + 1]\)] = poslist[\([i]\)] - 1], \[IndentingNewLine]{i, 1, nsteps}];\)\), "\[IndentingNewLine]", \(\(\(ListPlot[poslist, PlotJoined \[Rule] True]\)\(\[IndentingNewLine]\) \)\)}], "Input"], Cell["\<\ Do the random walk a bunch of times and determine the percentage of the time \ the drunk hits +100\ \>", "Text", FontSize->18], Cell[BoxData[{ \(nsteps = 7200; \ nwalks = 80; \ nvisitshome = 0;\), "\[IndentingNewLine]", \(\(maxlist = {};\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]poslist = Table[0, {i, 1, nsteps + 1}]; \[IndentingNewLine]Do[\[IndentingNewLine]x = Random[]; \[IndentingNewLine]If[x < 0.5, poslist[\([i + 1]\)] = poslist[\([i]\)] + 1, poslist[\([i + 1]\)] = poslist[\([i]\)] - 1], \[IndentingNewLine]{i, 1, nsteps}]; \[IndentingNewLine]maxlist\ = \ Append[maxlist, Max[poslist]]; \[IndentingNewLine]If[ Max[poslist] > 99, nvisitshome = nvisitshome + 1], \[IndentingNewLine]{j, 1, nwalks}\[IndentingNewLine]];\)\), "\[IndentingNewLine]", \(Print["\"]\), "\ \[IndentingNewLine]", \(maxlist\), "\[IndentingNewLine]", \(Print["\"]\), \ "\[IndentingNewLine]", \(N[nvisitshome/nwalks]\), "\[IndentingNewLine]", \(\)}], "Input"], Cell["\<\ Attempt #1 at random point in circle: random r from 0 to 1, random theta from 0 to 2 Pi\ \>", "Text", FontSize->18], Cell[BoxData[{\(xlist = Table[0, {5000}];\), "\[IndentingNewLine]", \(ylist = Table[0, {5000}];\), "\[IndentingNewLine]", \(Do[\[IndentingNewLine]r \ = Random[]; \[IndentingNewLine]theta = Random[]*2*Pi; \[IndentingNewLine]xlist[\([j]\)] = r*Cos[theta]; \[IndentingNewLine]ylist[\([j]\)] = r*Sin[theta], \[IndentingNewLine]{j, 1, 5000}];\), "\[IndentingNewLine]", \(xylist = Append[{xlist}, ylist];\), "\[IndentingNewLine]", \(xylist = Transpose[xylist];\), "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"xylist", ",", RowBox[{"AspectRatio", "\[Rule]", RowBox[{"Automatic", Cell[""]}]}]}], "]"}]}], "Input"], Cell[TextData[StyleBox["Faulty! (Biases center)", FontColor->RGBColor[1, 0, 0]]], "Text", CellOpen->False, FontSize->48], Cell["\<\ Attempt #2 at random point in circle: random x from -1 to 1, random y from -1 to 1, then scaled by Sqrt[1-x^2]\ \>", "Text", FontSize->18], Cell[BoxData[{\(xlist = Table[0, {5000}];\), "\[IndentingNewLine]", \(ylist = Table[0, {5000}];\), "\[IndentingNewLine]", \(Do[\[IndentingNewLine]x \ = 2*Random[] - 1; \[IndentingNewLine]y = 2*Random[] - 1; \[IndentingNewLine]y = y*Sqrt[1 - x^2]; \[IndentingNewLine]xlist[\([j]\)] = x; \[IndentingNewLine]ylist[\([j]\)] = y, \[IndentingNewLine]{j, 1, 5000}];\), "\[IndentingNewLine]", \(xylist = Append[{xlist}, ylist];\), "\[IndentingNewLine]", \(xylist = Transpose[xylist];\), "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"xylist", ",", RowBox[{"AspectRatio", "\[Rule]", RowBox[{"Automatic", Cell[""]}]}]}], "]"}]}], "Input"], Cell[TextData[StyleBox["Faulty! (Biases edges)", FontColor->RGBColor[1, 0, 0]]], "Text", CellOpen->False, FontSize->48], Cell["\<\ Attempt #3 at random point in circle: random x from -1 to 1, random y from -1 to 1, try again if outside circle\ \>", "Text", FontSize->18], Cell[BoxData[{\(xlist = Table[0, {5000}];\), "\[IndentingNewLine]", \(ylist = Table[0, {5000}];\), "\[IndentingNewLine]", \(Do[\[IndentingNewLine]x \ = 2*Random[] - 1; \[IndentingNewLine]y = 2*Random[] - 1; \[IndentingNewLine]While[x^2 + y^2 > 1, x = 2*Random[] - 1; \[IndentingNewLine]y = 2*Random[] - 1]; \[IndentingNewLine]xlist[\([j]\)] = x; \[IndentingNewLine]ylist[\([j]\)] = y, \[IndentingNewLine]{j, 1, 5000}];\), "\[IndentingNewLine]", \(xylist = Append[{xlist}, ylist];\), "\[IndentingNewLine]", \(xylist = Transpose[xylist];\), "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"xylist", ",", RowBox[{"AspectRatio", "\[Rule]", RowBox[{"Automatic", Cell[""]}]}]}], "]"}]}], "Input"], Cell[TextData[StyleBox["Yes!", FontColor->RGBColor[1, 0, 0]]], "Text", CellOpen->False, FontSize->48], Cell["\<\ Simulate how often 2 random points in the unit circle are farther than 1.2 \ from each other\ \>", "Text", FontSize->18], Cell[BoxData[{ \(\(n = 25000;\)\), "\[IndentingNewLine]", \(\(nfar = 0;\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]x1 = 2*Random[] - 1; \[IndentingNewLine]y1 = 2*Random[] - 1; \[IndentingNewLine]While[x1^2 + y1^2 > 1, x1 = 2*Random[] - 1; \[IndentingNewLine]y1 = 2*Random[] - 1]; \[IndentingNewLine]x2 = 2*Random[] - 1; \[IndentingNewLine]y2 = 2*Random[] - 1; \[IndentingNewLine]While[x2^2 + y2^2 > 1, x2 = 2*Random[] - 1; \[IndentingNewLine]y2 = 2*Random[] - 1]; \[IndentingNewLine]If[ Sqrt[\((x1 - x2)\)^2 + \((y1 - y2)\)^2] > 1.2, nfar = nfar + 1], \[IndentingNewLine]{j, 1, n}];\)\), "\[IndentingNewLine]", \(Print["\"]\), "\ \[IndentingNewLine]", \(N[nfar/n]\)}], "Input"], Cell["Volume of unit ball in R^n", "Text", FontSize->18], Cell[BoxData[ \(volball[dim_, nrandompoints_]\ := \[IndentingNewLine]Block[{ninball, sumofsquares, y, j, i}, \[IndentingNewLine]ninball = 0; \[IndentingNewLine]Do[\[IndentingNewLine]sumofsquares = 0; \[IndentingNewLine]Do[y = Random[]*2 - 1; sumofsquares = sumofsquares + y^2, {j, 1, dim}]; \[IndentingNewLine]If[sumofsquares\ < \ 1.0, ninball = ninball + 1], \[IndentingNewLine]{i, 1, nrandompoints}]; \[IndentingNewLine]\ N[ninball/ nrandompoints*2^\((dim)\)]\ \ \[IndentingNewLine]]\)], "Input"], Cell[BoxData[ \(Table[volball[dim, 4000], {dim, 2, 9}]\)], "Input"], Cell[BoxData[ \(Table[volball[dim, 8000], {dim, 2, 9}]\)], "Input"], Cell[BoxData[ \(Table[volball[dim, 16000], {dim, 2, 9}]\)], "Input"], Cell[BoxData[ \(Table[volball[dim, 32000], {dim, 2, 9}]\)], "Input"], Cell["An example of a non-uniform distribution:", "Text", FontSize->18], Cell[BoxData[{ \(\(bins = Table[0, {100}];\)\), "\[IndentingNewLine]", \(\(n = 15000;\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]y = Random[]; \[IndentingNewLine]x = \(-Log[ 1 - y]\); \[IndentingNewLine]z = Ceiling[x*10.0]; \[IndentingNewLine]If[z > 100, z = 100]; \[IndentingNewLine]bins[\([z]\)] = bins[\([z]\)] + 1, \[IndentingNewLine]{i, 1, n}];\)\), "\[IndentingNewLine]", \(<< Graphics`Graphics`\), "\[IndentingNewLine]", \(BarChart[10*bins/n, PlotRange \[Rule] {0, 1}]\)}], "Input"], Cell[BoxData[ \(Plot[Exp[\(-x\)], {x, 0, 10}, PlotRange \[Rule] {0, 1}]\)], "Input"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " also has a normal distribution built in" }], "Text", FontSize->18], Cell[BoxData[ \(<< Statistics`ContinuousDistributions`\)], "Input"], Cell[BoxData[ \(\(ndist = NormalDistribution[5, 2];\)\)], "Input"], Cell[BoxData[ \(\(x = Table[Random[ndist], {5000}];\)\)], "Input"], Cell[BoxData[ \(<< Graphics`Graphics`\)], "Input"], Cell[BoxData[ \(Histogram[x]\)], "Input"] }, FrontEndVersion->"4.2 for Microsoft Windows", ScreenRectangle->{{0, 1400}, {0, 967}}, WindowSize->{991, 629}, WindowMargins->{{161, Automatic}, {Automatic, 8}}, PrintingCopies->1, PrintingPageRange->{Automatic, Automatic}, Magnification->1.5 ] (******************************************************************* 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[1754, 51, 78, 1, 58, "Text"], Cell[1835, 54, 626, 12, 302, "Input"], Cell[2464, 68, 48, 1, 42, "Input"], Cell[2515, 71, 54, 1, 42, "Input"], Cell[2572, 74, 52, 1, 42, "Input"], Cell[2627, 77, 55, 1, 58, "Text"], Cell[2685, 80, 519, 9, 250, "Input"], Cell[3207, 91, 138, 4, 95, "Text"], Cell[3348, 97, 1106, 22, 484, "Input"], Cell[4457, 121, 128, 4, 95, "Text"], Cell[4588, 127, 720, 12, 309, "Input"], Cell[5311, 141, 127, 3, 28, "Text", CellOpen->False], Cell[5441, 146, 151, 4, 95, "Text"], Cell[5595, 152, 741, 12, 335, "Input"], Cell[6339, 166, 125, 3, 28, "Text", CellOpen->False], Cell[6467, 171, 152, 4, 95, "Text"], Cell[6622, 177, 814, 13, 361, "Input"], Cell[7439, 192, 107, 3, 28, "Text", CellOpen->False], Cell[7549, 197, 132, 4, 95, "Text"], Cell[7684, 203, 880, 16, 406, "Input"], Cell[8567, 221, 58, 1, 58, "Text"], Cell[8628, 224, 625, 11, 276, "Input"], Cell[9256, 237, 71, 1, 42, "Input"], Cell[9330, 240, 71, 1, 42, "Input"], Cell[9404, 243, 72, 1, 42, "Input"], Cell[9479, 246, 72, 1, 42, "Input"], Cell[9554, 249, 73, 1, 58, "Text"], Cell[9630, 252, 593, 11, 302, "Input"], Cell[10226, 265, 88, 1, 42, "Input"], Cell[10317, 268, 140, 5, 58, "Text"], Cell[10460, 275, 71, 1, 42, "Input"], Cell[10534, 278, 70, 1, 42, "Input"], Cell[10607, 281, 70, 1, 42, "Input"], Cell[10680, 284, 54, 1, 42, "Input"], Cell[10737, 287, 45, 1, 42, "Input"] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)