Generating a random letter a-z


Written on Apr 1 2023
💻
algorithms
javascript logo
javascript

Imagine yourself... playing a game in a cabin, something like scategories. You need to pick a random letter (case-insensitive) to determine which letter your answers have to start with. Here is an easy snipped that will run in almost any browser:

The easiest way to accomplish this is in javascript using the browsers runtime.

1String.fromCharCode(0|Math.random()*26+97)

Javascript exposes a nice function on `String` that allows us to get the string representation of a character code aka a number. It also provides the Math.random function to generate a random number.