<reponame>fimoziq/tutorials<gh_stars>100-1000
# -*- 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))

"""<center><h1>Thank you</h1></center>"""
# -*- 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))

"""<center><h1>Thank you</h1></center>"""
# -*- 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 ### Random numbers within a range #initialize the seed to 25 #generating random number between 10 and 20(both excluded) #generating random number between 10 and 20(both included) ### Random element from a sequence #initialize the seed to 2 #setting up the sequence #randomly choosing an element from the sequence ### Multiple random selections with different possibilities #initialize the seed to 25 #setting up the sequence #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 element from a sequence without replacement #initialize the seed to 25 #setting up the sequence #randomly choosing an element from the sequence #initialize the seed to 25 #setting up the sequence #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 ### Rearrange the sequence #initialize the seed to 25 #setting up the sequence #rearranging the order of elements of the list ### Floating-point random number #initialize the seed to 25 #random float number between 0 and 1 ### Real-valued distributions #initialize the seed to 25 #random float number between 10 and 20 (both included) #random float number mean 10 standard deviation 4 ## Generating pseudorandom numbers with Numpy #importing random module from numpy ### Uniform distributed floating values #initialize the seed to 25 #single uniformly distributed random number #initialize the seed to 25 #uniformly distributed random numbers of length 10: 1-D array #initialize the seed to 25 #uniformly distributed random numbers of 2 rows and 3 columns: 2-D array ### Normal distributed floating values #initialize the seed to 25 #single normally distributed random number #initialize the seed to 25 #normally distributed random numbers of length 10: 1-D array #initialize the seed to 25 #normally distributed random numbers of 2 rows and 3 columns: 2-D array ###  Uniformly distributed integers in a given range #initialize the seed to 25 #single uniformly distributed random integer between 10 and 20 #initialize the seed to 25 #uniformly distributed random integer between 0 to 100 of length 10: 1-D array #initialize the seed to 25 #uniformly distributed random integer between 0 to 100 of 2 rows and 3 columns: 2-D array ### Random elements from a defined list #initialize the seed to 25 #setting up the sequence #randomly choosing an element from the sequence #initialize the seed to 25 #setting up the sequence #randomly choosing elements from the sequence: 2-D array #initialize the seed to 25 #setting up the sequence #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 ### Binomial distributed values #initialize the seed to 25 #10 number of trials with probability of 0.5 each ### Poisson Distribution values #initialize the seed to 25 #rate 2 and size 10 ### Chi Square distribution #initialize the seed to 25 #degree of freedom 2 and size (2, 3) <center><h1>Thank you</h1></center>