Week 7
Pseudorandom numbers & jQuery Review

Math.random()

Open up your console and type in Math.random(), the JavaScript function for generating a “random number” between 0 and 1.

While this method does provide a number that you didn’t choose, it’s not actually random. It just does a good job of simulating randomness.

This is because all algorithmic random numbers can’t generate randomness, the way you can by chance in real life. Someone programmed the computer to do a function that goes through a set of number and returns a value from that list. While it may seem random, if you did it enough times, you’d begin to notice a pattern emerging.

In real life, however, you can generate truly random events. For example, you could do this by rolling a dice, spinning a roulette wheel, or by using the I-Ching…

The I-Ching (Book of Changes)



The I Ching (Also known as the Book of Changes, translates directly to “Word of God”) is an Ancient Chinese text dating back to ~1000 BC that was thought to be a way of communicating with divinities, and was used as an oracle.

To use the I Ching, the user obtained a random number by throwing coins or dice. Each side of the coin or the die is given a value (for example: heads is 1, tails is 2). Each value represents a line, for example: even numbers respond to a broken (yang) line and odd numbers respond to a strong, yin, line. You throw the coins 6 times and receive a line associated with each roll. These six numbers generate a hexagram.

More information:

Depending on your hexagram, you’ll look it up in the book. The book has 64 different hexagrams which you would then reference in the I Ching and read the interpretation. If you got the hexagram Lin, the meaning is “approach” or if you got the hexagram “Feng” that would mean abundance. Further interpretation from this would provide alternate readings, kind of like an oracle.


This is truly random, because it’s depending on things outside of anyone’s control, they just happen.

Throughout history, many artists have explored using randomness as a way of creating work. The artist, John Cage, used the I Ching to generate his piano compositions. Here is his work Music of Changes from 1951, performed by David Tudor.


John Cage (1912 – 1992) was an experimental composer and music theorist and he explored the idea of chance created music throughout his career. Through of many of his compositions, he questioned the concept of music and pushed it past the convention of formulaic melodies. His work sought to explore the meaning behind sounds, and brings a level of consciousness to your environment.


Anyway, imagine using the I Ching method to generate a random number. It seems a bit cumbersome. While Math.random() may not actually be random, it’s random enough for our purposes.

 

Using math.random()

As you saw earlier, math.random() generates a number between 0 and 1, but most times you’ll need a whole number. In order to do that, you can multiply the random number generated by math.random() by 10, and then round the number down using floor().

Random Integer (between 0 and another #)

Math.floor(Math.random()*10);