Expose yourself to as much randomness as possible.
~ Ben Casnocha
First i trust everyone is safe.
Second it is WEDNESDAY and that must mean a Snake_Byte or you are working in a startup because every day is WEDNESDAY in a startup!
i almost didn’t get this one done because well life happens but i want to remain true to the goals herewith to the best of my ability.
So in today’s
Snake_Byte we are going to cover Random and PseudoRandom Numbers. i really liked this one because it was more in line with scientific computing and numerical optimization.
random module in Python generates what is called pseudorandom numbers. It is in the vernacular a pseudorandom number generator (PRNG). This generation includes different types of distributions for said numbers.
So what is a pseudorandom number:
“A pseudorandom number generator (PRNG), also known as a deterministic random bit generator, is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers.” ~ Wikipedia
The important aspect here is: the properties approximate sequences of random numbers. So this means that it is statistically random even though it was generated by a deterministic response.
While i have used the
random module and have even generated various random number algorithms i learned something new in this blog. The pseudorandom number generator in Python uses an algorithm called the Mersenne Twister algorithm. The period of said algorithm is length 2**19937-1 for the 32 bit version and there is also a 64-bit version. The underlying implementation in C is both fast and thread-safe. The Mersenne Twister is one of the most extensively tested random number generators in existence. One issue though is that due to the deterministic nature of the algorithm it is not suitable for cryptographic methods.
Let us delve down into some code into the various
random module offerings, shall we?
i like using
%system in Jupyter Lab to create an interactive session. First we
import random. Lets look at
random.random() which returns a uniform distribution and when multiplied by a integer bounds it within that distribution range:
%system import random for i in range (5): x = random.random() * 100 print (x)
63.281889167063035 0.13679757425121286 47.697874648329 96.66882808709684 76.63300711554905
Next let us check out random.choice(seq) which returns a random element from the non-empty sequence seq. If
seq is empty, raises
for z in range (5): mySurfBoardlist = ["longboard", "shortboard", "boogieboard"] print(random.choice(mySurfBoardlist))
longboard boogieboard boogieboard longboard shortboard
Next let us look at
random.randrange(start, stop[, step]) which returns a randomly selected element from
range(start, stop, step). This is equivalent to
choice(range(start, stop, step)) but doesn’t actually build a
|start||Optional. An integer specifying at which position to start.|
|stop||Required. An integer specifying at which position to end.|
|step||Optional. An integer specifying the incrementation.|
for i in range (5): print(random.randrange(10, 100,1))
84 21 94 91 87
Now let us move on to some calls that you would use in signal processing, statistics or machine learning. The first one is
gauss() returns a gaussian distribution using the following mathematics:
Gaussian distribution (also known as normal distribution) is a bell-shaped curve (aka the bell curve), and it is assumed that during any measurement values will follow a normal distribution with an equal number of measurements above and below the mean value.
|sigma||the standard deviation|
|returns||a random gaussian distribution floating number|
# import the required libraries import random import matplotlib.pyplot as plt #set the inline magic %matplotlib inline # store the random numbers in a list nums =  mu = 100 sigma = 50 for i in range(100000): temp = random.gauss(mu, sigma) nums.append(temp) # plot the distribution plt.hist(nums, bins = 500, ec="red") plt.show()
There are several more parameters in the
random module, setter functions, seed functions and very complex statistical functions. Hit stack overflow and give it a try! Also it doesn’t hurt if you dust off that probability and statistics textbook!
As a last thought which came first the framework of entropy or the framework of randomness? As well as is everything truly random? i would love to hear your thought in the comments!
#iwishyouwater <- click here on this one!
Python In A Nutshell by Alex Martelli
M. Matsumoto and T. Nishimura, “Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator”, ACM Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3–30 1998
Muzak To Muzak To Blog By: Black Sabbath – The End: Live In Birmingham