Hacker News new | ask | show | jobs
by kazinator 2254 days ago
Classic BASIC implementation picked up on this idea. For instance some dialects had an INKEY$ variable which looks like a string, but magically reads console input.

GNU Bash has a magic variable RANDOM that produces random numbers. Assignments to it appear to be ignored.

In Algol, the name of the enclosing function acts as a variable to which you assign in order to produce the return value. For instance, see the assignment to B in the nested function here;

https://en.wikipedia.org/wiki/Man_or_boy_test

3 comments

> GNU Bash has a magic variable RANDOM that produces random numbers. Assignments to it appear to be ignored.

Assignments are used to seed¹ the random number generator². This isn't limited to just bash/zsh/etc, it is intrinsic to the process of generating random numbers. Being aware of this is useful as it poses a significant footgun. The /dev/random docs³ provide a nice overview of how it is often handled on various systems at the OS level to deal with reboots.

An example of the fun that this can produce can be seen in a Debian bug⁴, running a script that uses subshells and RANDOM with bash will behave very differently when using zsh. In this instance you have to hope that subshells aren't being used to create unique logs as an example.

1. https://en.m.wikipedia.org/wiki/Random_seed

2. https://en.m.wikipedia.org/wiki/Random_number_generator

3. https://en.m.wikipedia.org/wiki//dev/random

4. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=828180

I remember from GW-BASIC there being, ERR, ERL, CSRLIN, and probably a few others. C64 BASIC V2.0 had ST and TI/TI$.

BASIC's version of $RANDOM is RND(x), and I remember that the sign of the argument was the most important thing. Negative values seeded the PRNG and any positive argument got you the next random number. Some later dialects had a RANDOMIZE statement.

The funny thing is BASIC also had a couple functions that took input variables, but didn't do anything with them, like POS() and FRE(). I think FRE's argument may have been significant on a couple BASICs.

Flashback! 7 year old me named my rabbit after the inkey variable.