  We study random number generators (RNGs), both in the fixed to
variable-length (FVR) and the variable to fixed-length (VFR) regimes, in a
universal setting in which the input is a finite memory source of arbitrary
order and unknown parameters, with arbitrary input and output (finite) alphabet
sizes. Applying the method of types, we characterize essentially unique optimal
universal RNGs that maximize the expected output (respectively, minimize the
expected input) length in the FVR (respectively, VFR) case. For the FVR case,
the RNG studied is a generalization of Elias's scheme, while in the VFR case
the general scheme is new. We precisely characterize, up to an additive
constant, the corresponding expected lengths, which include second-order terms
similar to those encountered in universal data compression and universal
simulation. Furthermore, in the FVR case, we consider also a "twice-universal"
setting, in which the Markov order k of the input source is also unknown.
