In Bash there are some variables (like $RANDOM
) which are builtin functions. My understanding is that $RANDOM
uses the C function random
(e.g. man random
).
But I had to stumble across that information. What I wanted to be able to do was something like man $RANDOM
or type $RANDOM
(or even help $RANDOM
).
The problem with trying that is $RANDOM
gets evaluated to the actual random number :-)
So how do you identify what the implementation is for a special builtin variable like $RANDOM
? Other than sifting through Bash source code, which I just don't have the brain capacity for.
Surely there's a way to get the shell to indicate what implementation a special variable has (e.g. "this is a builtin variable that points to a C function of <N> name")
Or maybe there isn't? *shrugs*
Any help/info on this would be appreciated :-)
Thanks!
from man bash
, /RANDOM
RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its
special properties, even if it is subsequently reset.
manual doesn't say how it is implemented.
Otherwise from sources : variables.c shows that RANDOM is linked to function get_random
INIT_DYNAMIC_VAR ("RANDOM", (char *)NULL, get_random, assign_random);
which calls get_random_number
, seedrand
and brand
/* A linear congruential random number generator based on the example
one in the ANSI C standard. This one isn't very good, but a more
complicated one is overkill. */
/* Returns a pseudo-random number between 0 and 32767. */
static int
brand ()
{
/* From "Random number generators: good ones are hard to find",
Park and Miller, Communications of the ACM, vol. 31, no. 10,
October 1988, p. 1195. filtered through FreeBSD */
long h, l;
/* Can't seed with 0. */
if (rseed == 0)
rseed = 123459876;
h = rseed / 127773;
l = rseed % 127773;
rseed = 16807 * l - 2836 * h;
#if 0
if (rseed < 0)
rseed += 0x7fffffff;
#endif
return ((unsigned int)(rseed & 32767)); /* was % 32768 */
}