Function: random

random is a function defined in fns.c.

Signature

(random &optional LIMIT)

Documentation

Return a pseudo-random integer.

By default, return a fixnum; all fixnums are equally likely. With positive integer LIMIT, return random integer in interval [0,LIMIT). With argument t, set the random number seed from the system's entropy pool if available, otherwise from less-random volatile data such as the time. With a string argument, set the seed based on the string's contents.

See Info node (elisp)Random Numbers for more details.

Other relevant functions are documented in the number group.

View in manual

Probably introduced at or before Emacs version 17.

Shortdoc

;; number
(random 6)
    => 1

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  if (EQ (limit, Qt))
    init_random ();
  else if (STRINGP (limit))
    seed_random (SSDATA (limit), SBYTES (limit));
  else if (FIXNUMP (limit))
    {
      EMACS_INT lim = XFIXNUM (limit);
      if (lim <= 0)
        xsignal1 (Qargs_out_of_range, limit);
      return get_random_fixnum (lim);
    }
  else if (BIGNUMP (limit))
    {
      struct Lisp_Bignum *lim = XBIGNUM (limit);
      if (mpz_sgn (*bignum_val (lim)) <= 0)
        xsignal1 (Qargs_out_of_range, limit);
      return get_random_bignum (lim);
    }

  return make_ufixnum (get_random ());
}