Clive Thompson passionately advocates for Math.random() as a powerful and versatile line of JavaScript code that introduces serendipity and creativity in programming, particularly in cultural applications.
Abstract
In a thought-provoking piece, Clive Thompson champions Math.random() as a singularly impactful line of code, especially in the realm of creative coding. Thompson, a seasoned tech writer, illustrates how this JavaScript function excels at generating pseudo-random numbers, a task inherently challenging for humans due to our cognitive biases. He highlights the use of Math.random() in his own projects, such as a poetry-generating Twitter bot and a "Weird Old Book Finder," to demonstrate its ability to produce unexpected and enjoyable outcomes. Thompson also notes the function's practical utility in overcoming decision paralysis, as posited by the "Paradox of Choice" theory, by simplifying decision-making processes through random selection.
Opinions
Thompson believes Math.random() is a source of joy and creativity in programming, particularly in cultural projects.
He suggests that humans are inherently poor at generating randomness, making the Math.random() function a valuable tool.
The author values the element of surprise and the unique combinations that randomness can introduce to art and poetry, as evidenced by his Twitter bot's success.
Thompson points out that randomness has been historically significant in creative fields, citing the "N+7" technique in OULIPO poetry and its prevalence in video games.
He acknowledges the simplicity of implementing randomness in software, yet emphasizes its profound impact on the user experience.
Thompson views Math.random() as a playful and non-judgmental decision-maker that can help users navigate the overwhelming number of choices in the digital age.
He appreciates the "slot machine for culture" effect that randomness can create, making the discovery of content more engaging and less burdensome.
The answers were pretty fun, and most were the ones you’d expect (if you’re a developer, anyway). Some folks praised the while and for loops, since they’re cornerstones of how get computers to do repetitive work and “take care of the most boring part of our lives.” Other coders talked about the power of print statements (making the computer talk), if-else statements (automated decision-making!), or the import command, which lets you automatically phagocytose someone else’s open-source code and use it in your own app.
Awesome choices, for sure. But for me, there’s one command that brings me a lot of joy:
Random
Pseudo-random-number generators! Most programming languages have one. If you’re writing in Javascript or Node, as I often am, the function is …
Math.random()
I humbly nominate this as the most weirdly magical — and surprisingly useful — line of code in existence.
This is precisely why games of chance have historically relied on tools to generate pseudorandomness, like dice. Computers are our modern dice-tossers. One line of code gets you as many pseudorandom numbers as you need.
And it turns out that the “random” command is often, for me, incredibly useful.
When you’re making cultural apps, randomness can be a wonderfully creative force.
For example, for my twitterbot I hand-wrote over 900 lines of haiku-esque poetry. Three times a day, the bot picks three lines at random and smooshes ’em together.
The hard part of creating the bot was writing all those lines. I had to craft each such that it could stand on its own, yet also be open-ended enough to readily combine with any other. That was most of the work.
But software-wise? It was hilariously easy and uncomplex. It’s like four lines of code. A single Math.random() command does all the work.
Yet damn, it works pretty well! The bot’s been tweeting for almost five years, and I continue to surprised by the combinations that Math.random() produces, even though I personally wrote every line. The combos are generally ones I’d probably never have thought of myself.
But there’s another, perhaps subtler thing I find Math.random() useful for: Making a decision.
Specifically, I use it to overcome the “Paradox of Choice”.
That theory posits that when we’re offered a really broad range of options, it becomes harder to make a decision, and we’re less satisfied with the decision we finally make. It’s not a rock-solid theory, and has been disputed in recent years. But it certainly rings true to my own personal experience. If I go into a bookstore and behold a massive array of “recently released” titles, it sort of browns out my attention, and I drift away. I find it much easier to pick from a small table of 10 books curated by the bookstore staff.
So when I built the Weird Old Book Finder, I used randomness to sidestep the paradox of choice. I take your search query and pass it to the Google Books API; when the results come back, I filter for any titles published before the mid-1920s (which are thus in the public domain and immediately full-text readable).
The problem is, the Google Books API sends back up to 40 titles. That’s too many. I didn’t want to offer a list of choices — it might trigger a choice paradox!
So I just randomly pick one to send back to the user.
Again, it’s a dead simple technique, a single line of code. But it gives the search engine a playful quality. You can run the same search term several times and get back different books. It becomes a slot machine for culture.
Math.random() is also playful in in a way that humans normally aren’t, and maybe can’t be.
I mean, if you asked a bookseller or a librarian for a book on a subject, they wouldn’t tell you “eh, just go to the stack for that subject and grab, like, whatevs.” They’d feel too awkward to do that. Humans prefer to behave in ways that are meaningful and meaning-creating. They’d feel more comfortable helping guide you to make a choice.
But Math.random() has no such hangups. It’ll give you whatevs.
So, that’s my vote for the most powerful line of code: