From 93203514be8220dc3012cd80152fbabfeff4c0ab Mon Sep 17 00:00:00 2001 From: Sujan Shirol <71747522+sujanshirol@users.noreply.github.com> Date: Sun, 3 Jan 2021 21:57:54 +0530 Subject: [PATCH] Add files via upload --- ...umber_Generator_Tutorial_with_Python.ipynb | 1023 +++++++++++++++++ 1 file changed, 1023 insertions(+) create mode 100644 random-number-generator/Random_Number_Generator_Tutorial_with_Python.ipynb diff --git a/random-number-generator/Random_Number_Generator_Tutorial_with_Python.ipynb b/random-number-generator/Random_Number_Generator_Tutorial_with_Python.ipynb new file mode 100644 index 0000000..9527fbe --- /dev/null +++ b/random-number-generator/Random_Number_Generator_Tutorial_with_Python.ipynb @@ -0,0 +1,1023 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Random Number Generator Tutorial with Python.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "naANqJjn8wD1" + }, + "source": [ + "# Random Number Generator Tutorial with Python" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZEPAjtjS8-71" + }, + "source": [ + "## Generating pseudorandom numbers with Python standard library\r\n", + "\r\n", + "Python has a built-in module called random to generate a variety of pseudorandom numbers. Although it is recommended that this module should not be used for security purposes like cryptographic uses this will do for machine learning and data science. This module uses a PRNG called Mersenne Twister." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fuxpe_d79JCe" + }, + "source": [ + "### Importing module: random" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "v_XtrcTHbVLw" + }, + "source": [ + "import random" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x-ocR9mht0Nl" + }, + "source": [ + "### Random numbers within a range" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Jro9cAZydTQx", + "outputId": "d677b2d0-42f3-47b6-97ae-d34a4dbba491" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#generating random number between 10 and 20(both excluded)\r\n", + "print(random.randrange(10, 20))\r\n", + "\r\n", + "#generating random number between 10 and 20(both included)\r\n", + "print(random.randint(10, 20))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "16\n", + "10\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aiL805zcuI5x" + }, + "source": [ + "### Random element from a sequence" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "Sx6Y_kkNeh15", + "outputId": "640cd4ea-61e4-4431-833e-e16782b4c8f8" + }, + "source": [ + "#initialize the seed to 2\r\n", + "random.seed(2)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#randomly choosing an element from the sequence\r\n", + "random.choice(myseq)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'Towards'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 81 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rYmk9MNCumsG" + }, + "source": [ + "### Multiple random selections with different possibilities" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4092OaXthwnF", + "outputId": "0fdc6a5f-a55d-4b96-d6d5-09de0262230e" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#random selection of length 15\r\n", + "#10 time higher possibility of selecting 'Towards'\r\n", + "#5 time higher possibility of selecting 'AI'\r\n", + "#2 time higher possibility of selecting 'is'\r\n", + "#2 time higher possibility of selecting 1\r\n", + "random.choices(myseq, weights=[10, 5, 2, 2], k = 15)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Towards',\n", + " 1,\n", + " 'is',\n", + " 'Towards',\n", + " 'is',\n", + " 'AI',\n", + " 'Towards',\n", + " 1,\n", + " 'Towards',\n", + " 'Towards',\n", + " 'Towards',\n", + " 'AI',\n", + " 'Towards',\n", + " 'AI',\n", + " 'is']" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 82 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OV8qVvBhq8F6" + }, + "source": [ + "### Random element from a sequence without replacement" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uusp2UGnq-HZ", + "outputId": "333eca9d-8f66-47e4-bcdc-281f83359e04" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#randomly choosing an element from the sequence\r\n", + "random.sample(myseq, 2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 'Towards']" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 83 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 313 + }, + "id": "WEnN82S7uGnr", + "outputId": "01512b41-d525-476b-f1e1-8d3e728f84eb" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#randomly choosing an element from the sequence\r\n", + "#you are trying to choose 5 random elements from a sequence of lenth 4\r\n", + "#since the selection is without replacement it is not possible and hence the error\r\n", + "random.sample(myseq, 5)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "ValueError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;31m#randomly choosing an element from the sequence\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msample\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmyseq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/usr/lib/python3.6/random.py\u001b[0m in \u001b[0;36msample\u001b[0;34m(self, population, k)\u001b[0m\n\u001b[1;32m 318\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 319\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mk\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 320\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Sample larger than population or is negative\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 321\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 322\u001b[0m \u001b[0msetsize\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m21\u001b[0m \u001b[0;31m# size of a small set minus size of an empty list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: Sample larger than population or is negative" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jTvuhB-Uu4CT" + }, + "source": [ + "### Rearrange the sequence" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "PBcJ8c6Aj-5_", + "outputId": "95615455-270f-4cd4-f0a9-5201f6e0f886" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#rearranging the order of elements of the list\r\n", + "random.shuffle(myseq)\r\n", + "myseq" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['AI', 'is', 'Towards', 1]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 84 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BzYw8LWJvE6Q" + }, + "source": [ + "### Floating-point random number" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NaygYVbRowvM", + "outputId": "6fe86af1-a044-4558-9616-36ea6e9833ae" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#random float number between 0 and 1 \r\n", + "random.random()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.376962302390386" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 85 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MozWVFNWvR5y" + }, + "source": [ + "### Real-valued distributions" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "CICbelocpeim", + "outputId": "9e0163c9-2316-4ad9-f701-a807fd4ad287" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#random float number between 10 and 20 (both included)\r\n", + "print(random.uniform(10, 20))\r\n", + "\r\n", + "#random float number mean 10 standard deviation 4\r\n", + "print(random.gauss(10, 4))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "13.76962302390386\n", + "16.90247841037158\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vTyuEVdLhBr6" + }, + "source": [ + "## Generating pseudorandom numbers with Numpy" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "nCEvRzwLsmYl" + }, + "source": [ + "#importing random module from numpy\r\n", + "import numpy as np" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k9pesYZurQD0" + }, + "source": [ + "### Uniform distributed floating values" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "S-d70kuOhFCr", + "outputId": "0838d031-1ee2-435f-eabf-5faeeaeafc61" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#single uniformly distributed random number\r\n", + "np.random.rand()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.8701241366272119" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 88 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1VJ3YQvml1tm", + "outputId": "86c639cf-2400-47d7-d692-6f12a62744a7" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#uniformly distributed random numbers of length 10: 1-D array\r\n", + "np.random.rand(10)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0.87012414, 0.58227693, 0.27883894, 0.18591123, 0.41110013,\n", + " 0.11737555, 0.68496874, 0.43761106, 0.55622933, 0.36708032])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 89 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yPzSEP93nEuL", + "outputId": "73fe5b39-73f4-4973-94a7-c5085e4f85d4" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#uniformly distributed random numbers of 2 rows and 3 columns: 2-D array\r\n", + "np.random.rand(2, 3)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0.87012414, 0.58227693, 0.27883894],\n", + " [0.18591123, 0.41110013, 0.11737555]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 90 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "q6pR3S5nrYTT" + }, + "source": [ + "### Normal distributed floating values" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "W_RfRgjcpC-S", + "outputId": "ab2749dd-0420-4b62-ec49-82dd4396179e" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#single normally distributed random number\r\n", + "np.random.randn()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.22827308966608442" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 91 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wct1BQrwhN3X", + "outputId": "c5368bd0-975b-4838-a37d-8f479ff13f0f" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#normally distributed random numbers of length 10: 1-D array\r\n", + "np.random.randn(10)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 0.22827309, 1.0268903 , -0.83958485, -0.59118152, -0.9568883 ,\n", + " -0.22232569, -0.61991511, 1.83790458, -2.05323076, 0.86858305])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 92 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "iumrAseopF_C", + "outputId": "36f2b66d-c281-457b-f281-530ca23532de" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#normally distributed random numbers of 2 rows and 3 columns: 2-D array\r\n", + "np.random.randn(2, 3)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 0.22827309, 1.0268903 , -0.83958485],\n", + " [-0.59118152, -0.9568883 , -0.22232569]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 93 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "stgI0z-oszqh" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wIt3vyh5syUh" + }, + "source": [ + "### Uniformly distributed integers in a given range" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ooqNmqOKhqMU", + "outputId": "6345fed4-590f-4656-d99d-eb7d41f777bc" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#single uniformly distributed random integer between 10 and 20\r\n", + "np.random.randint(10, 20)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "14" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 94 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "96f2VbvpiAyl", + "outputId": "a9467f73-17ff-4649-f667-c0854ea1fc6e" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#uniformly distributed random integer between 0 to 100 of length 10: 1-D array\r\n", + "np.random.randint(100, size=(10))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 4, 62, 90, 15, 61, 23, 44, 50, 8, 28])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 95 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YeZz0M-ljiCb", + "outputId": "4e21759b-e569-423d-fc48-8d460b43cce7" + }, + "source": [ + "#initialize the seed to 25\r\n", + "np.random.seed(25)\r\n", + "\r\n", + "#uniformly distributed random integer between 0 to 100 of 2 rows and 3 columns: 2-D array\r\n", + "np.random.randint(100, size=(2, 3))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 4, 62, 90],\n", + " [15, 61, 23]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 96 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7HkS96a6z1VT" + }, + "source": [ + "### Random elements from a defined list" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "WFqxZWH1j5Gx", + "outputId": "d5e7cee0-b8a0-49e3-ade1-d3f265bc3cae" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#randomly choosing an element from the sequence\r\n", + "np.random.choice(myseq)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'Towards'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 97 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FgTh5Rjux2dc", + "outputId": "5ab7bdb2-3f15-4cae-d180-dcec2621c7de" + }, + "source": [ + "#initialize the seed to 25\r\n", + "random.seed(25)\r\n", + "\r\n", + "#setting up the sequence\r\n", + "myseq = ['Towards', 'AI', 'is', 1]\r\n", + "\r\n", + "#randomly choosing elements from the sequence: 2-D array\r\n", + "np.random.choice(myseq, size=(2, 3))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([['is', 'AI', 'Towards'],\n", + " ['Towards', 'Towards', 'AI']], dtype='

Thank you

" + ] + } + ] +} \ No newline at end of file