0

My Single Favorite Line Of Javascript

 2 years ago
source link: https://debugger.medium.com/my-single-favorite-line-of-javascript-304b2e9632ea
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

My Single Favorite Line Of Javascript

Math.random() unleashes serendipity and weirdness

0*4qpCl7gBgeLSJD70.jpg

A few years ago, someone on Quora asked “What’s the most powerful line of code ever written?”

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.

1*CxI0aDIkf2sKQjFHBH0BqA.jpeg
“red dice” by Jeff Jeremy

Math.random() is magical because it does something we humans are terrible at: Generating random numbers.

No-one’s quite sure why we’re so bad at this. It’s probably because we overthink things. For example, if you ask people to pick a random number from 1 to 20, they’ll disproportionately pick 17. Why? Probably because it’s odd and prime so it feels random; but since we reason/intuit our way to that conclusion, it’s anything but.

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.

I’m a hobbyist coder who particularly enjoys making odd little cultural projects — like this twitterbot that generates poetry three times a day, or my “Weird Old Book Finder” (which, as you may infer, finds you weird old books.)

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.

1*FtdsiwNYgkcSwkdmOtWMeg.jpeg
the clivebot

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.

Using randomness in poetry and art is not remotely my invention, of course. It’s got a venerable pedigree, going back to the predigital age (as with the “N+7” technique for substituting nouns in OULIPO poetry) and then going into overdrive with computers. You see it, too, in many video games, which use bits of often-simple randomness to make games endlessly replayable. Tons of creators love employing computational randomness.

As Matthew Siu puts it …

“Randomness as a tool for discovering unfamiliar territory”: Matthew Siu on Twitter

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.

1*FdEh6a1pDTR05OEyULGomQ.jpeg

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:

Math.random().


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK