{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Random Numbers Module\n", "[Official Documentation](https://docs.python.org/3/library/random.html)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### randint\n", "Gives you a random integer between from and to values, inclusive." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 2 3 1 1 2 0 2 3 2 0 0 2 2 0 3 3 2 1 2 3 0 2 2 2 " ] } ], "source": [ "for i in range (25):\n", " print(random.randint(0, 3), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### randrange\n", "Works similar to the range function -- gives you a random number between from and to-1, with optional step. \n", "From defaults to 0 if only 1 argument is given. \n", "Step defaults to 1 if only 2 arguments are given." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n", "3 6 6 6 6 3 3 3 0 0 6 6 3 0 6 0 3 0 6 6 6 6 3 3 6 " ] } ], "source": [ "print(random.randrange(100))\n", "\n", "for i in range (25):\n", " print(random.randrange(0, 9, 3), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### choice\n", "Returns one randomly chosen item from a sequence (list, tuple or string). Works for lists/tuples of integers, floats, strings or other objects. " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n", "Roby\n", "Darby\n", "Washington\n", "Hampton\n" ] } ], "source": [ "print(random.choice([3, 5, 7, 9, 11]))\n", "\n", "names = ['Roby', 'Matthews', 'Washington', 'Darby', 'Hampton']\n", "for i in range(4):\n", " print(random.choice(names))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-\n", "a c e b b a e d c c " ] } ], "source": [ "print(random.choice('bunch-of-letters'))\n", "\n", "material = 'brocade'\n", "for i in range(10):\n", " print(random.choice(material), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### choices\n", "Just like choice, but returns a list of n random choices, with replacement, so each pick is from the full sequence." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[9, 5, 3, 6, 5, 6, 5, 3, 1, 5, 10, 1, 4, 4, 10]\n", "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" ] } ], "source": [ "numbers = [n+1 for n in range(10)]\n", "my_picks = random.choices(numbers, k=15)\n", "print(my_picks)\n", "print(numbers)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Darby', 'Hampton']\n" ] } ], "source": [ "names = ['Roby', 'Matthews', 'Washington', 'Darby', 'Hampton']\n", "print(random.choices(names, k=2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also add weights if you want some items to have a better chance of being picked. Here, 1 is 4x more likely than 4 to be picked." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 1, 3, 4, 1, 1, 1, 3, 2, 2, 2, 4, 2, 1, 1, 3, 2, 3, 1, 3]\n" ] } ], "source": [ "numbers = [1,2,3,4]\n", "my_picks = random.choices(numbers, weights=[4,3,2,1], k=20)\n", "print(my_picks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use random.choices to generate random passwords \n", "First we pick a list of 8 random numbers between a and z on the ascii table, then we convert the numbers to ascii letters, then join them into a string." ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100, 109, 104, 100, 103, 102, 118, 121]\n", "dmhdgfvy\n" ] } ], "source": [ "picks = random.choices(range(ord('a'),ord('z')), k=8)\n", "print(picks)\n", "picks = [chr(i) for i in picks]\n", "print(''.join(picks))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a random password generator that uses all upper and lower case letters and numbers." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Z81Hw3uk\n" ] } ], "source": [ "import string\n", "all_chars = string.ascii_lowercase + string.ascii_uppercase + string.digits\n", "pw = ''.join(random.choices(all_chars, k=8))\n", "print(pw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sample\n", "Just like choices, but without replacement. \n", "Useful for picking lottery winners or bingo numbers. \n", "Returned list is in the order they were picked." ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['green', 'pink']\n" ] } ], "source": [ "colors = ['red', 'blue', 'green', 'aqua', 'pink', 'black']\n", "picks = random.sample(colors, k=2)\n", "print(picks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the range function as an argument will not give you any duplicate picks." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[18, 38, 20, 50, 1]\n" ] } ], "source": [ "picks = random.sample(range(1,51), k=5)\n", "print(picks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### shuffle\n", "Shuffle any sequence into random order. \n", "This is an in-place shuffle, and it doesn't return anything." ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8]\n", "None\n", "[2, 6, 8, 1, 4, 7, 5, 3]\n" ] } ], "source": [ "numbers = [1, 2, 3, 4, 5, 6, 7, 8]\n", "print(numbers)\n", "print(random.shuffle(numbers))\n", "\n", "random.shuffle(numbers)\n", "print(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### random.random()\n", "Random floating point values between 0.0 and 1.0." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8424897774160051\n", "0.9016594664191279\n", "0.5162849368345925\n", "0.021852081927422384\n", "0.5740618908246983\n", "0.6539291129848911\n" ] } ], "source": [ "print(random.random())\n", "\n", "for i in range(5):\n", " print(random.random())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### uniform (from, to)\n", "Random float between a range of values" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.5032833557221568\n", "10.31258224982709\n", "9.431820659293221\n", "10.4390639618008\n", "9.6906814789157\n", "10.559354593909362\n" ] } ], "source": [ "print(random.uniform(2.1, 4.3))\n", "\n", "for i in range(5):\n", " print(random.uniform(9.4, 10.7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }