Debugger

Debugger is a former publication from Medium about consumer technology and gadgets. Currently inactive and not taking submissions.

Follow publication

My Single Favorite Line Of Javascript

Math.random() unleashes serendipity and weirdness

Clive Thompson
Debugger
Published in
5 min readMar 6, 2022

--

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.

“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

--

--

Debugger
Debugger

Published in Debugger

Debugger is a former publication from Medium about consumer technology and gadgets. Currently inactive and not taking submissions.

Clive Thompson
Clive Thompson

Written by Clive Thompson

I write 2X a week on tech, science, culture — and how those collide. Writer at NYT mag/Wired; author, “Coders”. @clive@saturation.social clive@clivethompson.net

Responses (2)

Write a response