{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Pull in the packages we need:*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "require(stats)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parameters\n",
    "\n",
    "* **cpd** - Checkins per day. Defaults to 20.\n",
    "\n",
    "* **days** - Number of days to simulate.  Defaults to 10000, or roughly\n",
    "    40 working years.\n",
    "\n",
    "* **winSz** - Size of the commit window as a fraction of `workSec`. Defaults\n",
    "    to 0.01%, which is roughly 3 seconds for the default 8-hour work day, a\n",
    "    a long-ish commit time for Fossil.\n",
    "\n",
    "* **workSec** - Seconds in working day, defaulting to 8 hours.  This value\n",
    "    only affects the reporting output, not the results of the underlying\n",
    "    simulation.  It's a scaling parameter to humanize the results.\n",
    "    \n",
    "* **spread** - Adjustment factor in computing the standard deviation for our \n",
    "    normally-distributed random numbers. The default gives \"nice\" distributions,\n",
    "    spread over the working day with a low chance of generating values outside\n",
    "    the working day.\n",
    "    \n",
    "    The smaller this value gets (&lt;4), the more spread out the checkins, and\n",
    "    so the lower the chance of collisions.  You might want to decrease it a bit\n",
    "    to simulate early and late workers.\n",
    "    \n",
    "    As you increase this value (&gt;4) you're simulating a work environment\n",
    "    where people tend to check their work in closer and closer to mid-day,\n",
    "    which increases the chance of collision."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "collisions <- function(\n",
    "        cpd = 20,\n",
    "        days = 10000,\n",
    "        winSz = 0.01 / 100,\n",
    "        workSec = 8 * 60 * 60,\n",
    "        spread = 4)\n",
    "{\n",
    "    cat(\"Running simulation...\\n\")\n",
    "\n",
    "    day = 0\n",
    "    collisions = 0\n",
    "    winSec = workSec * winSz\n",
    "    mean = workSec / 2\n",
    "    sd = workSec / spread\n",
    "\n",
    "    while (day < days) {\n",
    "        # Create the commit time vector as random values in a normal\n",
    "        # distribution.\n",
    "        times = sort(rnorm(cpd, mean, sd))\n",
    "\n",
    "        # Are there any pairs in the time vector that are within the\n",
    "        # passed window size?  If so, that's a Fossil checkin collision.\n",
    "        i = 1\n",
    "        while (i < cpd) {\n",
    "            if (abs(times[i] - times[i + 1]) < winSec) {\n",
    "                collisions = collisions + 1\n",
    "            }\n",
    "            i = i + 1\n",
    "        }\n",
    "        \n",
    "        day = day + 1\n",
    "    }\n",
    "    \n",
    "    cat(\"Found\", collisions, \"collisions in\", days, (workSec / 3600),\n",
    "        \"hour working days with\", winSec, \"second windows.\\n\")\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Run the following cell, possibly changing parameters documented above:*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running simulation...\n",
      "Found 422 collisions in 10000 8 hour working days with 2.88 second windows.\n"
     ]
    }
   ],
   "source": [
    "collisions()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.3.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
