Malkin approached me on Discord, asking me how to pick a random letter. I’ve been using random numbers in CAOS with the RAND command for like forever, but I hadn’t found a reason yet to pick random letters. The question made me think, though.
Of course, there’s the old-fashioned, boring and bulky-code-way of just picking a random number between 1 and number-of-letters-in-the-alphabet. Then you can set a variable to a letter depending on the value of that number, in an if-elif-elif-elif-elif-… scenario.
Surely there should be a cleaner/easier way!
Recently I learned more about the CHAR variable thanks to Amaikokonut’s blog post over at Naturing :: Nurturing. I noticed char has two commands, one that returns a variable and one that sets a variable.
The command returning the character returns the ASCII number for that character.
I wondered if the one setting a variable also uses the ASCII code for a character: Yup!
So, all that’s left is to figure out the ASCII numbers of the characters you want to use.
For the characters A to Z, that’s 65 to 90. For the characters a to z, that’s 97 to 122.
So if you want a character between a to z, the resulting code would be this:
sets va00 “a”
setv va01 rand 97 122
char va00 1 va01
Nice and clean, right? 😀