top of page

Python and Chaos

  • Writer: Ian Vicino
    Ian Vicino
  • Sep 29, 2023
  • 5 min read

I’ve been reading a book written by James Gleik called Chaos Making A New Science and it has inspired me to look at everything differently. I am still reading the book, and I am in no way an expert in chaos theory, but by reading it I am inspired to take up a pen and paper and learn more mathematics, or pick up my computer and program in pseudorandom number generator based on the chaos theory. Which brings me to the reason for this article. I am not going to discuss the book in length, but highly encourage you to read it yourself. (I bought my copy for $1 at the thrift store.) Instead, my goal is to explain why you should learn to program a computer so you can experiment and explore fields of science like chaos theory on your own, wherever you find yourself.


After I was introduced to the Python programming language while in graduate school, I was hooked. It opened up so many possibilities I had never dreamed about before. No longer was a computer a pile of circuitry I could complete my homework with, or use to play Minecraft. I was no longer dependent on computer applications made by others. After I learned to code the computer became my avenue to creativity. I could now write program applications to do any tedious and/or repetitive task or even create my own artificial intelligence programs. The computer became a vehicle for my experimentation.


After I learned to program I began learning how to create a machine-learning application. That is an application that learns how to do a task on its own. The problem is the machine learning algorithms, in order to truly understand how they work, required a higher level of mathematics than what I was exposed to while in school. My biology degrees did not require any mathematics above Calculus 2. So, I found some lessons online and learned some differential equations. Python essentially opened my eyes to routes of knowledge that were previously hidden from my view.


Throughout school, even after my undergraduate degree in Biochemistry, I was afraid of math. I respected math but was never encouraged to explore it past what was required for a degree. To me, math meant examinations and grades and I hated grades. That mental link and societal pressures caused me to fear math. As for computers, I was not exposed to much computation while at school other than learning how to type on the computer.


I did join a robotics club while at school, but only joined it because my friends were part of it. They actually knew how to program a computer or in this case a robot. I was just there to help put the robot together. Thinking back on it, at the time I didn’t think I was smart enough to learn how to code. Now I know I was wrong, but hindsight is 20/20. Leave it that school did not encourage me to explore math and computation without the threat of bad grades.


Now I know the joy of programming an application and learning a new form of mathematics. I have learned how to learn on my own by reading textbooks or finding videos online. And I encourage you to do the same. I encourage you, dear reader, to learn how to code or learn something you were afraid to do before. If you were or are afraid of math, find a free textbook or video lesson online and try your hand at learning or relearning the math without the fear of grades. That is the benefit of learning on your own, there are no examinations or grades unless you are looking for those. Or, if you want to learn how to code using the Python programming language, check out my tutorials on this website and reach out to me. I can schedule some time to have a one-on-one Python lesson with you. I can teach you starting from the ground up, as fast or slowly as you want to go. Also, trust me, there will be no examinations or grades. Leave a comment with your email address and we can go from there.


Before I end my article, let us go back to what started this discussion. I was inspired to explore chaos theory by making a pseudorandom number generator with Python, so let us do that.


(To repeat I am not an expert on chaos theory and don’t want to spend forever programming this application. I just want to use it to inspire others to learn to code and experiment on their own.)


We are going to use the logarithmic growth function: rx(1-x) where r is the rate of growth and x is a decimal between 0 and 1 indicating the initial population percentage, where 1 means the population is at the maximum value. For example, if we have a population of rabbits that doubles their population every year, and the initial population is half the maximum population, the equation would look like 2*0.5(1-0.5). The solution of this equation will be the population of rabbits next year, which in this case would be 0.5, or in other words it would stay as half the population.


This function is important because if you graph the population percentage (x) by the rate of growth (r) you get an interesting graph called the bifurcation plot.

If the growth rate goes above 3.6, the population changes chaotically every year, for the most part. We are going to use this chaotic growth to create our pseudorandom number generator. It will not truly be random because its output depends on what numbers we initialize it with, but it is a good place to start playing with chaos theory.


I first started by figuring out what rate I needed to get a seemingly random output, so I used chatGPT to give me some Python code that outputs the bifurcation graph which you can see above as well as the code to produce the graph. The first code it gave me did not work, so I asked again and the second set of code worked. So, if you are using chatGPT for coding help, make sure the code works before you rely on it too much.


After looking at the graph, I figured a rate of 3.7 would yield a random enough output. As for the initial population, I made that a question to ask the user. The user would input any number between 0 and 1 indicating the initial population percentage. That left only one question, how many random numbers are needed by the user, which I wrote as the first question prompt.


The final code can be seen below. Try it out for yourself to see if you can improve the pseudorandom number generator, maybe make it so it outputs only single digits as random numbers instead of the long floats produced by my code. If you don’t know how to code yet, leave me a comment and I can teach you all you want to learn. Also, if you need help with any biology courses, I am also an expert in biology, so reach out to me for that as well.


Enjoy your experimentation and share this article with a friend. It would be much appreciated!



# pseudorandom  Number Generator using Chaos Theory

# Logistic Growth equation
# Xi+1 = RXi(1-Xi)



entry = int(input('How many random numbers do you want? '))
X = float(input('Write a decimal between 0 and 1 indicating the initial population percentage (maximum of 1) ')) # Xi is the percentage of rabbits, for instance, with a maximum of 1 or 100%
R = 3.7

def PRNG():
    x = R*X*(1-X)
    for num in range(entry):
        x = R*x*(1-x)
        print(x)
    return x 

prng = PRNG()
print('Your final variable is: ', prng)

Example of the random numbers generated by my code:


Comments


bottom of page