{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Random numbers\n", "\n", "## Objectives\n", "\n", "* Understand the limitations of random number generators\n", "* Know how to seed and use a random number generator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Intro: Grading\n", "\n", "* Make sure you read the comments!\n", "* JiTT comments should be usually available before class starts\n", "* Week 1 homework has been graded\n", " - Week 3 homework due date pushed back to end of day today" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Week 1 homework\n", "* Non-runnable code\n", " - I edited the notebook, -2 penalty\n", " - Make sure you leave all code syntactically valid\n", "* Dot in filename: Please don't do this\n", "* Docstring: Many of you forgot (I will only require something that specific if I specifically request it in the problem)\n", "* Return values: A function should \"return\" whatever it is asked to produce\n", "* Some confusion about lists vs. length of lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A word about Magics\n", "\n", "## Project plans\n", "\n", "Friday is project selection day! We'll go over the possible projects then. You'll have a little time to think about it before selecting one. **Please send me or post on Blackboard your project selection ideas!**\n", "\n", "We'll be discussing setting up project files (instead of just using notebooks) on that day too, probably.\n", "\n", "## Cheat sheets\n", "\n", "Posted on Blackboard." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initial words\n", "\n", "Most of what we'll be covering is \"random numbers\" for data science, not cryptography." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def lin_cong(c, a, M, r_i):\n", " return (a * r_i + c) % M" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lin_cong(c=1, a=4, M=9, r_i=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lin_cong(c=1, a=4, M=9, r_i=4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def iterator():\n", " yield 1\n", " yield 2\n", " yield 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for item in iterator():\n", " print(item)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tuple(iterator())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def lin_cong_iter(c, a, M, r_0):\n", " r_i = r_0\n", "\n", " while True:\n", " yield r_i\n", " r_i = lin_cong(c, a, M, r_i)\n", " if r_i == r_0:\n", " return" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(lin_cong_iter(1, 4, 9, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(i / 8 for i in lin_cong_iter(1, 4, 9, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try it out:\n", "\n", "Set `a=57, c=1, M=256, r_0=10` (like in the book) and see what happens." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vals = list(lin_cong_iter(a=57, c=1, M=256, r_0=10))\n", "print(len(vals))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "real_rand = np.random.randint(0, 256, 256)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(1, 2, figsize=(12, 4))\n", "axs[0].plot(vals[::2], vals[1::2], \"+\")\n", "axs[1].plot(real_rand[::2], real_rand[1::2], \"+\")\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(1, 2, figsize=(16, 4))\n", "axs[0].plot(range(256), vals, \"x-\")\n", "axs[1].plot(range(256), real_rand, \"x-\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python random numbers\n", "\n", "Since the book mentions Python's random numbers, let's look at that first. Once you know how they work, it really doesn't matter which library you use except for speed and simplicity (and then go with Numpy, of course)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.seed(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " print(random.randint(0, 256))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numpy random numbers\n", "\n", "Let's try the same with Numpy:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.randint(0, 256, 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "state1 = np.random.RandomState(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "state1.randint(0, 256)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(42)\n", "rsamp = np.random.rand(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rsamp[:100]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(rsamp, \".\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "N = len(rsamp)\n", "moment1 = 1 / N * np.sum(rsamp ** 1)\n", "moment2 = 1 / N * np.sum(rsamp ** 2)\n", "moment3 = 1 / N * np.sum(rsamp ** 3)\n", "print(f\"moment 1: {moment1} == {1/(1+1)} +/- {1/np.sqrt(N)}\")\n", "print(f\"moment 2: {moment2} == {1/(1+2)} +/- {1/np.sqrt(N)}\")\n", "print(f\"moment 3: {moment3} == {1/(1+3)} +/- {1/np.sqrt(N)}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.roll(np.arange(10), -2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 / N * np.sum(rsamp * np.roll(rsamp, 1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 / N * np.sum(rsamp * np.roll(rsamp, 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 / N * np.sum(rsamp * np.roll(rsamp, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 / N * np.sum(rsamp * np.roll(rsamp, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "ax.plot(rsamp[::2], rsamp[1::2], \"+\")\n", "plt.show()" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 2 }