Coding, Community Projects, JavaScript, Milestones, Simon makes gamez, Simon's Own Code

Simon’s Math Games in p5.js

Simon loves the Maths Is Fun website and has borrowed a couple of ideas for cool games from there. He wrote the code completely on his own, from scratch. Below is a video where he presents his Connect games:

Published on October 30, 2019

I’ve gone Connect Crazy!
This project is inspired by MathsIsFun, which has a lot of variations on the classic game Connect Four, the code is entirely mine.
Classic Game of Connect Four: https://editor.p5js.org/simontiger/full/TISsTqZ8D
And then I’ve made Connect Three: https://editor.p5js.org/simontiger/full/c30Oqd4Qf
And Connect Five: https://editor.p5js.org/simontiger/full/I6Digth0A
Then I’ve also made a version called Drop, where if the bottom row fills up, the whole board drops down one row:
https://editor.p5js.org/simontiger/full/Ysu2yvh1x
https://editor.p5js.org/simontiger/full/gb3gVSd5K

screen shot of Simon’s Connect 5 game

Another game Simon built was Bulls and Cows (trying to guess a sequence of letters): https://editor.p5js.org/simontiger/full/3bC9j3501 Link to Simon’s code: https://editor.p5js.org/simontiger/sketches/3bC9j3501

Yet another one was a Reaction Time test! You can test your reaction time by clicking anywhere on the screen as soon as the circle changes color: https://editor.p5js.org/simontiger/full/Gzv094mgzM Link to Simon’s code: https://editor.p5js.org/simontiger/sketches/Gzv094mgzM

screenshot of the Reaction Test

And last but not least, an unfinished project of building a Checkers game:

screenshot of Simon’s yet unfinished Checkers game sketch

Coding, Community Projects, Contributing, Experiments, JavaScript, live stream, Machine Learning, Milestones, Physics, Simon's Own Code

Simon’s Random Number Generator

This one’s back from mid-October, forgot to post here.

Simon created a random number generator that generates a frequency, and then picks it back up. Then, it calculates the error between the generated frequency and the picked up frequency. This is one of my community contributions for a Coding Train challenge: https://thecodingtrain.com/CodingChallenges/151-ukulele-tuner.html

Link to project: https://editor.p5js.org/simontiger/sketches/eOXdkP7tz
Link to the random number plots: https://www.wolframcloud.com/env/monajune0/ukalele%20tuner%20generated%20random%20number%20analysis.nb
Link to Daniel Shiffman’s live stream featured at the beginning of this vid: https://youtu.be/jKHgVdyC55M

plot of the random numbers generated by Simon’s ukulele tuner random number generator (plotted in Wolfram Mathematica)
Coding, Computer Science, Good Reads, JavaScript, Milestones, Murderous Maths, Notes on everyday life, Set the beautiful mind free, Simon's Own Code, Simon's sketch book

Simon’s first steps in Stephen Wolfram’s Computational Universe

Simon has been enjoying Stephen Wolfram’s huge volume called A New Kind of Science and is generally growingly fascinated with Wolfram’s visionary ideas about the computational universe. We have been reading the 1500-page A New Kind of Science every night for several weeks now, Simon voraciously soaking up the behaviour of hundreds of simple programs like cellular automata.

Wolfram’s main message is that, contrary to our intuition, simple rules can result in complex and often seemingly random behaviour and since humanity now has the computer as a tool to study and simulate that behaviour, it could open a beautiful new alternative to the existing models used in science. According to Wolfram, we may soon realise that the mathematical models we are currently using, based on equations and constraints instead of simple rules, are merely a historical artefact. I’m amazed at how much this is in line with Simon’s own tentative thoughts he was sharing with me earlier this year, about how maths will be taken over by computer science and how algorithms are a more powerful tool than equations. When he came up with those ideas he hadn’t discovered Wolfram’s research and philosophy yet, he used to only know Wolfram as the creator of Wolfram Mathematica and the Wolfram language, both of which Simon greatly admires for being so advanced.

Last night, Simon was watching a TED talk Stephen Wolfram gave in 2010 about the possibilities of computing the much aspired theory of everything, but not in the traditional mathematical way. “It’s about the universe!” Simon whispered to me wide-eyed, when I came to the living room to fetch him. “Mom, and you know who was in the audience there? Benoit Mandelbrot!” (Simon knows Mandelbrot died the same year, he is intrigued by the fact that his and Mandelbrot’s lifetimes have actually overlapped by one year).

We have been informed by the World Science Scholars program that Stephen Wolfram will be one of the professors preparing a course for this year’s scholars cohort, so Simon will have the unique experience of taking that course and engaging in a live session with Stephen Wolfram. It is breathtaking, a chance to connect with someone who is much older, renowned and accomplished, and at the same time so like-minded, a soulmate.

Inspired by reading Stephen Wolfram, Simon has revisited the world of cellular automata and Turing machines, and created a few beautiful Langton’s Ants:

Link to Simon’s sketch on p5: https://editor.p5js.org/simontiger/sketches/sHa6d-AFf
Simon especially likes the last example in the video: “I think of it as triangular houses surrounding a flower garden!”

Simon has also watched a talk by Stephen Wolfram for MIT course 6.S099: Artificial General Intelligence. He said it had things in it about Wolfram Alpha that he didn’t know yet.

Coding, Contributing, Geometry Joys, Math Tricks, Murderous Maths, Python, Simon teaching, Simon's Own Code, Simon's sketch book

Why the Golden Ratio and not -1/the Golden Ratio?

Take any real number and call it x. Then plug it into the equation f(x) = 1 + 1/x and keep doing it many times in a row, plugging the result back into the equation.

At some point you will see that you arrive at a value that will become stable and not change anymore. And that value will be… φ, the golden ratio!

But this equation also has another answer, -1/φ. If you plug that value into the equation, it will be the same, too. The real magic happens once you have rounded the -1/φ down (or up), i.e. once what you plug into the equation is no longer exactly -1/φ. What happens is that, if you keep going, you will eventually reach… φ as your answer!

Simon saw this interesting fact in a video by 3Blue1Brown and then came up with a proof as to why it happens.

Simon also sketched his proof in GeoGebra: https://www.geogebra.org/classic/zxmqdspb

Coding, Murderous Maths, Python, Simon teaching, Simon's Own Code, Simon's sketch book

Prime Generation Algorithm in Python

Simon has written a code in Python that generates primes using the finite list from Euclid’s proof that there are infinitely many primes. “Starting with one prime (2) the code uses the finite list to generate a couple more numbers that aren’t in the list but are primes. It may not even get to all the primes in the long run!” There is only one problem with Simon’s algorithm…

Simon has written down Euclid’s proof in his own words first https://imgur.com/ML2tI6n
and then decided to program it in Python.

Resources:
https://www.programiz.com/python-programming/methods/list/remove
https://www.geeksforgeeks.org/iterate-over-a-set-in-python/
https://www.youtube.com/watch?v=OWJCfOvochA
https://numbermatics.com/n/10650056950807/
https://defuse.ca/big-number-calculator.htm

Coding, English and Text-Based Data, Python, Simon teaching, Simon's Own Code

It takes the sun to the ground, and violet on the observer’s eye.

Simon writes:

This amazing sentence is generated by a Markov Text-Generation Algorithm. What is a Markov Algorithm? Simply put, it generates the rules from a source text, and it generates a new text that also follows those rules. The rules are often called the Markov Blanket, and the new text is also called the Markov Chain. OK, how does this all work?

Let’s take an example: let’s consider the source text to be “Hello, world!”. Then we pick a number called the order. The higher the number, the more sense the text makes. We’ll pick 1 for the first examples, we’ll examine what happens with higher numbers later.

Then we generate the Markov Blanket. This is a deterministic process. We start from the beginning: “H”. So we put H in our Markov Blanket. Then we come across “e”. So we put e in our Markov Blanket, but to specify that it’s next from H, we connect H to e by an arrow. Then we stumble on “l”. So we put l in our Markov Blanket, but again, to specify that it’s next from e, we connect e to l by an arrow.

Now, here’s where it gets interesting. What’s next? Well, it’s “l” again. So now we connect l to itself, by an arrow. This is interesting because it’s no longer a straight line!

And we keep going. Once we’re done, our Markov Blanket will look something like this:

Once we’ve created our Markov Blanket, then we start generating the Markov Chain from it. Unlike the Markov Blanket, generating the Markov Chain is a stochastic process.

This is just a process of wandering about the Markov Blanket, and noting down where we go. One way to do this, is just to start from the beginning, and follow the path. And whenever we come across some sort of fork, we just spin a wheel to see where we go next. For example, here are some possible Markov Chains:

Held!
Helld!
Hellld!
Helorld!
Hello, world!
Helllo, wo, wo, world!

That was an easy one, so let’s do something more complex with code.

First, just an interface to enter in the text, and the order:

text = "" # Variable to hold the text

print("Type your text here (type END to end it):")

while True:
  line = input("") # Read a line of text from standard input
  if line != "END": text += line + "\n" # If we didn't enter END, add that line to the text
  else: break # If we entered END, signify that the text has ended

text = text[:len(text)-1] # Remove the last line break

order = int(input('Type the order (how much it makes sense) here: '))

input("Generate me a beautiful text") # Just to make it dramatic, print this message, and ask the user to hit ENTER to proceed

Next, the Markov Blanket. Here, we store it in a dictionary, and store every possible next letter in a list:

def markov_blanket(text, order):
  result = {} # The Markov Blanket

  for i in range(len(text) - order + 1): # For every n-gram
    ngram = ""
    for off in range(order):
      ngram += text[i+off]
    
    if not ngram in result: # If we didn't see it yet
      result[ngram] = []
    if i < len(text) - order: # If we didn't reach the end
      result[ngram].append(text[i+order]) # Add the next letter as a possibility
  
  return result # Give the result back

Huh? What is this code?

This is what happens when we pick a number >1. Then, instead of making the Markov Blanket for every character, you make it for every couple of characters.

For example, if we pick 2, then we make the Markov Blanket for the 1st and 2nd letter, the 2nd and 3rd, the 3rd and 4th, the 4th and 5th, and so on. When we generate the Markov Chain, we squash the ngrams that we visit together. So next, the Markov Chain:

def markov_chain(blanket):
  keys = blanket.keys()
  ngram = random.choice(list(keys)) # Starting Point
  new_text = ngram
  while True:
    try:
      nxt = random.choice(blanket[ngram]) # Choose a next letter
      new_text += nxt # Add it to the text
      ngram += nxt # Add it to the ngram and remove the 1st character
      ngram = ngram[1:]
    except IndexError: # If we can't choose a next letter, maybe because there is none
      break
  return new_text # Give the result back

# Now that we know how to do the whole thing, do the whole thing!
new_text = markov_chain(markov_blanket(text, order), num)
print(new_text) # Print the new text out

OK, now let’s run this:

Type your text here (type END to end it):
A rainbow is a meteorological phenomenon that is caused by reflection, refraction and dispersion of light in water droplets resulting in a spectrum of light appearing in the sky. It takes the form of a multicoloured circular arc. Rainbows caused by sunlight always appear in the section of sky directly opposite the sun.
Rainbows can be full circles. However, the observer normally sees only an arc formed by illuminated droplets above the ground, and centered on a line from the sun to the observer's eye.
In a primary rainbow, the arc shows red on the outer part and violet on the inner side. This rainbow is caused by light being refracted when entering a droplet of water, then reflected inside on the back of the droplet and refracted again when leaving it.
In a double rainbow, a second arc is seen outside the primary arc, and has the order of its colours reversed, with red on the inner side of the arc. This is caused by the light being reflectedtwice on the inside of the droplet before leaving it.
END
Type the order (how much it makes sense) here: 5
Generate me a beautiful text

And……..it..stops.

Why did it do that?

You see, this is not such a good method. What if our program generated a Markov Blanket that didn’t have an end? Well, our program wouldn’t even get to the end, and it would just wander around and around and around, and never give us a result! Or even if it did, it would be infinite!

So how do we avoid this?

Well, we set another much bigger number , let’s say 5000, to be a callout value. If we don’t get to the end within 5000 steps, we give up and output early. Let’s run this again…

And now, it doesn’t stop anymore! Snippets of example generated text:

It takes the sun to the ground, and violet on the observer’s eye.

This rainbow, a second arc formed by illuminated droplets resulting it.
In a primary rainbow is a meteorological phenomenon the back of the ground, and has the sky. It takes the order of its coloured circles. However, the sun.

Rainbow, a second arc shows red on a line from the section of light in water droplet and has the sun.

In a double rainbow is caused by illuminated droplet on the outer part and refracted when leaving in a spectrum of a multicoloured circles. However, the droplet of water droplets resulting it.
In a double rainbow is a meteorological phenomenon the droplets resulting in a spectrum of a multicoloured circular arc. Rainbow is caused by the inner side the observer’s eye

Play with this project online at: https://repl.it/@simontiger/Markov-Text

Coding, JavaScript, Murderous Maths, Physics, Simon teaching, Simon's Own Code, Simon's sketch book

Heat Equation Visualization

A visual solution to Fourier’s heat equation in p5. Play with the two versions online:
https://editor.p5js.org/simontiger/present/EaHr9886H
https://editor.p5js.org/simontiger/sketches/EaHr9886H

https://editor.p5js.org/simontiger/present/ruN8CQV77
https://editor.p5js.org/simontiger/sketches/ruN8CQV77

Inspired by 3Blue1Brown’s Differential Equations series.

Coding, Computer Science, JavaScript, Simon teaching, Simon's Own Code, Simon's sketch book

Back to the sorting algorithms: Beadsort (and a short lecture about the generator function)

Link to the project: https://editor.p5js.org/simontiger/sketches/7gLA0u4KF
Made my Beadsort step-by-step with a generator function! https://editor.p5js.org/simontiger/full/ilZXV9Dp0 (Scroll down to see the “Next” button!) Code: https://editor.p5js.org/simontiger/sketches/ilZXV9Dp0
The video also contains a bonus tutorial about what a generator function is!