Pure Programmer
Blue Matrix


Cluster Map

Math Library

While the arithmetic and logic operators represent the basic operations that our [[CPU]] can perform, there are many other common math functions that com in handy. Since they are so common, programming languages usually have a math library that provides these functions. Logarithms, trigonometry and random number generation are just of few of the types of functions typically provided.

Math Constants

Math constants provide commonly used mathematical constanst to the highest precesion available. Some of the more useful math constants are summarized below.

Python Math Constants
Constant Description
math.eEuler's constant [[ℯ]], base of the natural logarithm
math.pi[[π]], Ratio of a circle's circumference to its diameter

Math Functions

These most useful math functions are summarized below.

Python Math Functions
Function Description
abs(x)[[Absolute value]] of x
math.acos(x)[[Arc cosine]] of x, result is in the range [0,π] [[Radians]]
math.asin(x)[[Arc sine]] of x, result is in the range [-π/2,π/2] [[Radians]]
math.atan(x)[[Arc tangent]] of x, result is in the range [-π/2,π/2] [[Radians]]
math.atan2(y,x)Angle θ from the conversion of [[rectangular coordinates]] (x,y),
result is in the range [-π,π] [[Radians]]
math.ceil(x)Smallest integer value greater than or equal to x
math.cos(x)[[Cosine]] of x (in [[Radians]])
math.exp(x)[[ℯ]] rasied to the power x, i.e. ℯx
math.floor(x)Largest integer less than x
math.log(x)[[Natural logarithm]] of x
max(x,y)Larger of x and y
min(x,y)Smaller of x and y
math.pow(x,y)x raised to the power y, i.e. xy
random.random()[[Pseudorandom]] number on the interval [0,1)
round(x)Nearest integer to x
math.sin(x)[[Sine]] of x (in [[Radians]])
math.sqrt(x)[[Square root]] of x
math.tan(x)[[Tangent]] of x (in [[Radians]])

Be sure to add the following import statements to the top of your program in order to use these math functions and constants.
from Utils import *
import math
import random

The program below illustrates the use of the floating point math functions.

Math1.py
#!/usr/bin/env python3;
###############################################################################
# This program demonstrates the math library.
# 
# Copyright © 2016 Richard Lesh.  All rights reserved.
###############################################################################

import Utils
import math
import random

# Begin Main
a = math.pi / 6
b = math.pi / 4
c = -a * 2
d = -b * 2
e = math.e

print("pi = {0:f}".format(math.pi))
print("e = {0:f}".format(math.e))

# abs, floor, ceil, round, trunc, min, max
print("abs({0:f}) = {1:f}".format(a, abs(a)))
print("abs({0:f}) = {1:f}".format(c, abs(c)))
print("floor({0:f}) = {1:f}".format(a, math.floor(a)))
print("floor({0:f}) = {1:f}".format(c, math.floor(c)))
print("ceil({0:f}) = {1:f}".format(a, math.ceil(a)))
print("ceil({0:f}) = {1:f}".format(c, math.ceil(c)))
print("round({0:f}) = {1:f}".format(a, round(a)))
print("round({0:f}) = {1:f}".format(c, round(c)))
print("trunc({0:f}) = {1:f}".format(a, math.trunc(a)))
print("trunc({0:f}) = {1:f}".format(c, math.trunc(c)))
print("min({0:f}, {1:f}) = {2:f}".format(a, c, min(a, c)))
print("max({0:f}, {1:f}) = {2:f}".format(a, c, max(a, c)))

# sin, cos, tan, atan, atan2, acos, asin
print("sin({0:f}) = {1:f}".format(a, math.sin(a)))
print("sin({0:f}) = {1:f}".format(b, math.sin(b)))
print("sin({0:f}) = {1:f}".format(c, math.sin(c)))
print("sin({0:f}) = {1:f}".format(d, math.sin(d)))
print("cos({0:f}) = {1:f}".format(a, math.cos(a)))
print("cos({0:f}) = {1:f}".format(b, math.cos(b)))
print("cos({0:f}) = {1:f}".format(c, math.cos(c)))
print("cos({0:f}) = {1:f}".format(d, math.cos(d)))
print("tan({0:f}) = {1:f}".format(a, math.tan(a)))
print("tan({0:f}) = {1:f}".format(b, math.tan(b)))
print("tan({0:f}) = {1:f}".format(c, math.tan(c)))
print("asin({0:f}) = {1:f}".format(math.sin(a), math.asin(math.sin(a))))
print("asin({0:f}) = {1:f}".format(math.sin(b), math.asin(math.sin(b))))
print("asin({0:f}) = {1:f}".format(math.sin(c), math.asin(math.sin(c))))
print("asin({0:f}) = {1:f}".format(math.sin(d), math.asin(math.sin(d))))
print("acos({0:f}) = {1:f}".format(math.cos(a), math.acos(math.cos(a))))
print("acos({0:f}) = {1:f}".format(math.cos(b), math.acos(math.cos(b))))
print("acos({0:f}) = {1:f}".format(math.cos(c), math.acos(math.cos(c))))
print("acos({0:f}) = {1:f}".format(math.cos(d), math.acos(math.cos(d))))
print("atan({0:f}) = {1:f}".format(math.tan(a), math.atan(math.tan(a))))
print("atan({0:f}) = {1:f}".format(math.tan(b), math.atan(math.tan(b))))
print("atan({0:f}) = {1:f}".format(math.tan(c), math.atan(math.tan(c))))
# 45 degrees
print("atan2({0:f}, {1:f}) = {2:f}".format(1.0, 1.0, math.atan2(1.0, 1.0)))
# 30 degrees
print("atan2({0:f}, {1:f}) = {2:f}".format(1.0, math.sqrt(3.0), math.atan2(1.0, math.sqrt(3.0))))

# sinh, cosh, tanh, atanh, acosh, asinh
print("sinh({0:f}) = {1:f}".format(a, math.sinh(a)))
print("sinh({0:f}) = {1:f}".format(b, math.sinh(b)))
print("sinh({0:f}) = {1:f}".format(c, math.sinh(c)))
print("sinh({0:f}) = {1:f}".format(d, math.sinh(d)))
print("cosh({0:f}) = {1:f}".format(a, math.cosh(a)))
print("cosh({0:f}) = {1:f}".format(b, math.cosh(b)))
print("cosh({0:f}) = {1:f}".format(c, math.cosh(c)))
print("cosh({0:f}) = {1:f}".format(d, math.cosh(d)))
print("tanh({0:f}) = {1:f}".format(a, math.tanh(a)))
print("tanh({0:f}) = {1:f}".format(b, math.tanh(b)))
print("tanh({0:f}) = {1:f}".format(c, math.tanh(c)))
print("tanh({0:f}) = {1:f}".format(d, math.tanh(d)))
print("asinh({0:f}) = {1:f}".format(math.sinh(a), math.asinh(math.sinh(a))))
print("asinh({0:f}) = {1:f}".format(math.sinh(b), math.asinh(math.sinh(b))))
print("asinh({0:f}) = {1:f}".format(math.sinh(c), math.asinh(math.sinh(c))))
print("asinh({0:f}) = {1:f}".format(math.sinh(d), math.asinh(math.sinh(d))))
print("acosh({0:f}) = {1:f}".format(math.cosh(a), math.acosh(math.cosh(a))))
print("acosh({0:f}) = {1:f}".format(math.cosh(b), math.acosh(math.cosh(b))))
print("acosh({0:f}) = {1:f}".format(math.cosh(c), math.acosh(math.cosh(c))))
print("acosh({0:f}) = {1:f}".format(math.cosh(d), math.acosh(math.cosh(d))))
print("atanh({0:f}) = {1:f}".format(math.tanh(a), math.atanh(math.tanh(a))))
print("atanh({0:f}) = {1:f}".format(math.tanh(b), math.atanh(math.tanh(b))))
print("atanh({0:f}) = {1:f}".format(math.tanh(c), math.atanh(math.tanh(c))))
print("atanh({0:f}) = {1:f}".format(math.tanh(d), math.atanh(math.tanh(d))))

# log, log10, exp, pow, sqrt
print("log({0:f}) = {1:f}".format(a, math.log(a)))
print("log({0:f}) = {1:f}".format(b, math.log(b)))
print("log({0:f}) = {1:f}".format(-c, math.log(-c)))
print("log({0:f}) = {1:f}".format(-d, math.log(-d)))
print("log({0:f}) = {1:f}".format(e, math.log(e)))
print("log10({0:f}) = {1:f}".format(a, math.log10(a)))
print("log10({0:f}) = {1:f}".format(b, math.log10(b)))
print("log10({0:f}) = {1:f}".format(-c, math.log10(-c)))
print("log10({0:f}) = {1:f}".format(-d, math.log10(-d)))
print("log10({0:f}) = {1:f}".format(e, math.log10(e)))
print("exp({0:f}) = {1:f}".format(0.5, math.exp(0.5)))
print("exp({0:f}) = {1:f}".format(1.0, math.exp(1.0)))
print("exp({0:f}) = {1:f}".format(2.0, math.exp(2.0)))
print("pow({0:f}, {1:f}) = {2:f}".format(10.0, 0.5, math.pow(10.0, 0.5)))
print("pow({0:f}, {1:f}) = {2:f}".format(10.0, 1.0, math.pow(10.0, 1.0)))
print("pow({0:f}, {1:f}) = {2:f}".format(10.0, 2.0, math.pow(10.0, 2.0)))
print("sqrt({0:f}) = {1:f}".format(0.5, math.sqrt(0.5)))
print("sqrt({0:f}) = {1:f}".format(2.0, math.sqrt(2.0)))
print("sqrt({0:f}) = {1:f}".format(10.0, math.sqrt(10.0)))

# random numbers
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))

Output
$ python3 Math1.py pi = 3.141593 e = 2.718282 abs(0.523599) = 0.523599 abs(-1.047198) = 1.047198 floor(0.523599) = 0.000000 floor(-1.047198) = -2.000000 ceil(0.523599) = 1.000000 ceil(-1.047198) = -1.000000 round(0.523599) = 1.000000 round(-1.047198) = -1.000000 trunc(0.523599) = 0.000000 trunc(-1.047198) = -1.000000 min(0.523599, -1.047198) = -1.047198 max(0.523599, -1.047198) = 0.523599 sin(0.523599) = 0.500000 sin(0.785398) = 0.707107 sin(-1.047198) = -0.866025 sin(-1.570796) = -1.000000 cos(0.523599) = 0.866025 cos(0.785398) = 0.707107 cos(-1.047198) = 0.500000 cos(-1.570796) = 0.000000 tan(0.523599) = 0.577350 tan(0.785398) = 1.000000 tan(-1.047198) = -1.732051 asin(0.500000) = 0.523599 asin(0.707107) = 0.785398 asin(-0.866025) = -1.047198 asin(-1.000000) = -1.570796 acos(0.866025) = 0.523599 acos(0.707107) = 0.785398 acos(0.500000) = 1.047198 acos(0.000000) = 1.570796 atan(0.577350) = 0.523599 atan(1.000000) = 0.785398 atan(-1.732051) = -1.047198 atan2(1.000000, 1.000000) = 0.785398 atan2(1.000000, 1.732051) = 0.523599 sinh(0.523599) = 0.547853 sinh(0.785398) = 0.868671 sinh(-1.047198) = -1.249367 sinh(-1.570796) = -2.301299 cosh(0.523599) = 1.140238 cosh(0.785398) = 1.324609 cosh(-1.047198) = 1.600287 cosh(-1.570796) = 2.509178 tanh(0.523599) = 0.480473 tanh(0.785398) = 0.655794 tanh(-1.047198) = -0.780714 tanh(-1.570796) = -0.917152 asinh(0.547853) = 0.523599 asinh(0.868671) = 0.785398 asinh(-1.249367) = -1.047198 asinh(-2.301299) = -1.570796 acosh(1.140238) = 0.523599 acosh(1.324609) = 0.785398 acosh(1.600287) = 1.047198 acosh(2.509178) = 1.570796 atanh(0.480473) = 0.523599 atanh(0.655794) = 0.785398 atanh(-0.780714) = -1.047198 atanh(-0.917152) = -1.570796 log(0.523599) = -0.647030 log(0.785398) = -0.241564 log(1.047198) = 0.046118 log(1.570796) = 0.451583 log(2.718282) = 1.000000 log10(0.523599) = -0.281001 log10(0.785398) = -0.104910 log10(1.047198) = 0.020029 log10(1.570796) = 0.196120 log10(2.718282) = 0.434294 exp(0.500000) = 1.648721 exp(1.000000) = 2.718282 exp(2.000000) = 7.389056 pow(10.000000, 0.500000) = 3.162278 pow(10.000000, 1.000000) = 10.000000 pow(10.000000, 2.000000) = 100.000000 sqrt(0.500000) = 0.707107 sqrt(2.000000) = 1.414214 sqrt(10.000000) = 3.162278 random() = 0.210281 random() = 0.076644 random() = 0.937783

The program below illustrates the use of the integer math and random number functions.

Math2.py
#!/usr/bin/env python3;
###############################################################################
# This program demonstrates the math integer functions.
# 
# Copyright © 2020 Richard Lesh.  All rights reserved.
###############################################################################

import Utils
import math
import random

# Begin Main
a = 5
b = 10
c = -2

# abs, floor, ceil, round, trunc, min, max
print("abs({0:d}) = {1:d}".format(a, abs(a)))
print("abs({0:d}) = {1:d}".format(c, abs(c)))
print("min({0:d}, {1:d}) = {2:d}".format(a, b, min(a, b)))
print("max({0:d}, {1:d}) = {2:d}".format(a, b, max(a, b)))
print("min({0:d}, {1:d}) = {2:d}".format(b, c, min(b, c)))
print("max({0:d}, {1:d}) = {2:d}".format(b, c, max(b, c)))

# random numbers
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))

Output
$ python3 Math2.py abs(5) = 5 abs(-2) = 2 min(5, 10) = 5 max(5, 10) = 10 min(10, -2) = -2 max(10, -2) = 10 random(5) = 0 random(5) = 2 random(5) = 4 random(5) = 1 random(5) = 0 random(10) = 3 random(10) = 6 random(10) = 6 random(10) = 2 random(10) = 2 random(2) = 0 random(2) = 0 random(2) = 0 random(2) = 1 random(2) = 0 random() = 0.960084 random() = 0.352941 random() = 0.339514 random() = 0.670385 random() = 0.017732

Random Numbers

Random number generation is an important technique needed for simulations and games. Computers can't actually generate true random numbers, so we have to settle for [[pseudorandom]] numbers, i.e. numbers generated deterministically but hopefully in an unpredictable manner.

In modern Python, random number generation is usually done with the random module from the standard library.

For the three most common cases of uniform integers, uniform floating point numbers, and normally distributed ([[Gaussian]]) floating point numbers, Python provides functions such as:

  1. uniform integers → randint() or randrange()
  2. uniform floating point numbers → random()
  3. normal (Gaussian) floating point numbers → gauss() or normalvariate()

For our example program we generate 10 numbers from each of the three distributions. Notice how each time we run the program, we will usually get different values.

RandomNumbers.py
import random

print("Uniform integers in [1, 6]")
for i in range(10):
    print(random.randint(1, 6))

print("Uniform floats in [0.0, 1.0)")
for i in range(10):
    print(random.random())

print("Standard Normal")
for i in range(10):
    print(random.gauss(0.0, 1.0))
Output
$ python3 RandomNumbers.py Uniform integers in [1, 6] 3 1 4 5 6 4 2 6 1 5 Uniform floats in [0.0, 1.0) 0.9505424240207292 0.16120265735011186 0.2865732042994441 0.7923487920536413 0.9248781873469002 0.5697218964310974 0.14192994773086787 0.3635631912900119 0.2773123699369422 0.007725452814186573 Standard Normal -0.680856331483455 0.5111367396529101 1.7912763435862686 0.8100160976573731 -1.271927926406831 0.47256558773156415 -1.783541584593209 -0.0665908690040314 -1.5321894615752123 -0.2941554940135625

The uniform integer generator can produce integers uniformly in a range such as: [1, 6].

In Python, integer bounds are often expressed using inclusive endpoints. So to simulate a six-sided die we would write: random.randint(1, 6).

This means both endpoints are included, so the possible values are 1 through 6. That makes it perfect for dice rolls, random indices, and similar cases.

The uniform floating point generator produces numbers uniformly in the range: [0.0, 1.0).

This means that only the low endpoint 0.0 is included. The high endpoint 1.0 will not be generated, i.e. low <= random < high. You can scale this output to another range [low, high) with the function: random.uniform(low, high)

That is commonly used for probabilities, simulation, [[Monte Carlo Method]], and scaling into another range.

The Gaussian generator produces values from a normal distribution with mean = 0.0 and standard deviation = 1.0. That is called the standard normal distribution.

Most values cluster near the mean, and larger positive or negative values become less likely.

For example, to model exam scores centered around 75 with a standard deviation of 10, we can scale and shift the standard normal value like this: random.gauss(75.0, 10.0).

Deterministic vs Non-Deterministic Seeding

If you want the same random sequence every run, use a fixed seed such as 12345. This is useful for debugging and testing.

random.seed(12345)

If you want variation between runs, Python automatically seeds the generator when the program starts.

Whichever technique you choose, only seed the generator once. Do not recreate and re-seed it repeatedly in a loop.

Random Number Functions

The random module provides several useful functions:

Cryptographically Secure Random Numbers

For security-sensitive work such as tokens, passwords, or keys, do not use the standard random module.

Instead use the secrets module, which is designed for cryptographic use.

import secrets

token = secrets.token_hex(16)
print(token)

This produces much stronger random values suitable for security-related purposes.

Questions

Projects

More ★'s indicate higher difficulty level.

References