From 959e411b742ec6c1af7c7b41e2727485d008510b Mon Sep 17 00:00:00 2001 From: Sujan Shirol <71747522+sujanshirol@users.noreply.github.com> Date: Sun, 3 Jan 2021 22:04:05 +0530 Subject: [PATCH] Add files via upload --- ...m_number_generator_tutorial_with_python.py | 233 ++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 random-number-generator/random_number_generator_tutorial_with_python.py diff --git a/random-number-generator/random_number_generator_tutorial_with_python.py b/random-number-generator/random_number_generator_tutorial_with_python.py new file mode 100644 index 0000000..d853d63 --- /dev/null +++ b/random-number-generator/random_number_generator_tutorial_with_python.py @@ -0,0 +1,233 @@ +# -*- coding: utf-8 -*- +"""Random Number Generator Tutorial with Python.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/1qKiX3ztczxxXNSlC0IKtSr8ND4UILE-G + +# Random Number Generator Tutorial with Python + +## Generating pseudorandom numbers with Python standard library + +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. + +### Importing module: random +""" + +import random + +"""### Random numbers within a range""" + +#initialize the seed to 25 +random.seed(25) + +#generating random number between 10 and 20(both excluded) +print(random.randrange(10, 20)) + +#generating random number between 10 and 20(both included) +print(random.randint(10, 20)) + +"""### Random element from a sequence""" + +#initialize the seed to 2 +random.seed(2) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#randomly choosing an element from the sequence +random.choice(myseq) + +"""### Multiple random selections with different possibilities""" + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#random selection of length 15 +#10 time higher possibility of selecting 'Towards' +#5 time higher possibility of selecting 'AI' +#2 time higher possibility of selecting 'is' +#2 time higher possibility of selecting 1 +random.choices(myseq, weights=[10, 5, 2, 2], k = 15) + +"""### Random element from a sequence without replacement""" + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#randomly choosing an element from the sequence +random.sample(myseq, 2) + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#randomly choosing an element from the sequence +#you are trying to choose 5 random elements from a sequence of lenth 4 +#since the selection is without replacement it is not possible and hence the error +random.sample(myseq, 5) + +"""### Rearrange the sequence""" + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#rearranging the order of elements of the list +random.shuffle(myseq) +myseq + +"""### Floating-point random number""" + +#initialize the seed to 25 +random.seed(25) + +#random float number between 0 and 1 +random.random() + +"""### Real-valued distributions""" + +#initialize the seed to 25 +random.seed(25) + +#random float number between 10 and 20 (both included) +print(random.uniform(10, 20)) + +#random float number mean 10 standard deviation 4 +print(random.gauss(10, 4)) + +"""## Generating pseudorandom numbers with Numpy""" + +#importing random module from numpy +import numpy as np + +"""### Uniform distributed floating values""" + +#initialize the seed to 25 +np.random.seed(25) + +#single uniformly distributed random number +np.random.rand() + +#initialize the seed to 25 +np.random.seed(25) + +#uniformly distributed random numbers of length 10: 1-D array +np.random.rand(10) + +#initialize the seed to 25 +np.random.seed(25) + +#uniformly distributed random numbers of 2 rows and 3 columns: 2-D array +np.random.rand(2, 3) + +"""### Normal distributed floating values""" + +#initialize the seed to 25 +np.random.seed(25) + +#single normally distributed random number +np.random.randn() + +#initialize the seed to 25 +np.random.seed(25) + +#normally distributed random numbers of length 10: 1-D array +np.random.randn(10) + +#initialize the seed to 25 +np.random.seed(25) + +#normally distributed random numbers of 2 rows and 3 columns: 2-D array +np.random.randn(2, 3) + +"""### Uniformly distributed integers in a given range""" + +#initialize the seed to 25 +np.random.seed(25) + +#single uniformly distributed random integer between 10 and 20 +np.random.randint(10, 20) + +#initialize the seed to 25 +np.random.seed(25) + +#uniformly distributed random integer between 0 to 100 of length 10: 1-D array +np.random.randint(100, size=(10)) + +#initialize the seed to 25 +np.random.seed(25) + +#uniformly distributed random integer between 0 to 100 of 2 rows and 3 columns: 2-D array +np.random.randint(100, size=(2, 3)) + +"""### Random elements from a defined list""" + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#randomly choosing an element from the sequence +np.random.choice(myseq) + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#randomly choosing elements from the sequence: 2-D array +np.random.choice(myseq, size=(2, 3)) + +#initialize the seed to 25 +random.seed(25) + +#setting up the sequence +myseq = ['Towards', 'AI', 'is', 1] + +#randomly choosing elements from the sequence with defined probabilities +#The probability for the value to be 'Towards' is set to be 0.1 +#The probability for the value to be 'AI' is set to be 0.6 +#The probability for the value to be 'is' is set to be 0.05 +#The probability for the value to be 1 is set to be 0.25 +#0.1 + 0.6 + 0.05 + 0.25 = 1 +np.random.choice(myseq, p=[0.1, 0.6, 0.05, 0.25], size=(2, 3)) + +"""### Binomial distributed values""" + +#initialize the seed to 25 +np.random.seed(25) + +#10 number of trials with probability of 0.5 each +np.random.binomial(n=10, p=0.5, size=10) + +"""### Poisson Distribution values""" + +#initialize the seed to 25 +np.random.seed(25) + +#rate 2 and size 10 +np.random.poisson(lam=2, size=10) + +"""### Chi Square distribution""" + +#initialize the seed to 25 +np.random.seed(25) + +#degree of freedom 2 and size (2, 3) +np.random.chisquare(df=2, size=(2, 3)) + +"""

Thank you

""" \ No newline at end of file