Library

  • Library: a library is a collection of code from an external source that can be used to add functions to a program(It convenience writing programs). For example, math is a library that have function of math, one of the function is square root a number which is written in "sqrt".

Example of use of library

import math
math.comb(12,10)
66

This code returns you the number of ways to choose k items from n items without repetition and without order.

Randomization

-The random library allows you to get a random number from a list, like rolling a dice.

For example, if you want to find a number from 1 to 10.

import random
random.randint(1,10)

Different method of library

seed() | Initialize the random number generator

getstate() | Returns the current internal state of the random number generator

setstate() | Restores the internal state of the random number generator

getrandbits() | Returns a number representing the random bits

randrange() | Returns a random number between the given range

randint() | Returns a random number between the given range

choice() | Returns a random element from the given sequence

choices() | Returns a list with a random selection from the given sequence

shuffle() | Takes a sequence and returns the sequence in a random order

sample() | Returns a given sample of a sequence

random() | Returns a random float number between 0 and 1

uniform() | Returns a random float number between two given parameters

betavariate() | Returns a random float number between 0 and 1 based on the Beta distribution (used in statistics)

expovariate() | Returns a random float number based on the Exponential distribution (used in statistics)

gammavariate() | Returns a random float number based on the Gamma distribution (used in statistics)

gauss() | Returns a random float number based on the Gaussian distribution (used in probability theories)

lognormvariate() | Returns a random float number based on a log-normal distribution (used in probability theories)

normalvariate() | Returns a random float number based on the normal distribution (used in probability theories)

vonmisesvariate() | Returns a random float number based on the von Mises distribution (used in directional statistics)

paretovariate() | Returns a random float number based on the Pareto distribution (used in probability theories)

weibullvariate() | Returns a random float number based on the Weibull distribution (used in statistics)

3.15.2

  • random (a,b) will give you a integer between a and b.

Rolling coin example:

import random
# choose either 1 or 2
flip = random.randint(1,2)

# make head and tail equals to 1 or two
if flip == 1:
    print("Head")
else:
    print("Tail")
Tail