Random Numbers

This page describes the random-number generator in PYTHIA and how it can be replaced by an external one.

Internal random numbers

The Rndm class generates random numbers, using the Marsaglia-Zaman-Tsang algorithm [Mar90]. It is purely static, i.e. only exists in one copy, so that one cannot run several copies, each with the same random number sequence, by mistake.

Random numbers R uniformly distributed in 0 < R < 1 are obtained with

   Rndm::flat();
There are also methods to generate according to an exponential, to x * exp(-x), to a Gaussian, or picked among a set of possibilites, which make use of flat().

If the random number generator is not initialized before, it will be so the first time it is asked to generate a random number, and then with the default seed, 19780503. You can initialize, or reinitialize, with your own choice of seed with a

   Rndm::init(seed);
Here values 0 < seed < 900 000 000 gives so many different random number sequences, while seed = 0 will call the Stdlib time(0) function to provide a "random" seed, and seed < 0 will revert back to the default seed.

The Pythia class defines a flag and a mode, that allows the seed to be set in the Pythia::init call. That would be the standard way for a user to pick the random number sequence in a run.

External random numbers

RndmEngine is a base class for the external handling of random-number generation. The user-written derived class is called if a pointer to it has been handed in with the pythia.rndmEnginePtr() method. Since the default Marsaglia-Zaman-Tsang algorithm is quite good, chances are that any replacement would be a step down, but this may still be required by consistency with other program elements in big experimental frameworks.

There is only one pure virtual method in RndmEngine, to generate one random number flat in the range between 0 and 1:

  virtual double flat() = 0;
Note that methods for initialization are not provided in the base class, in part since input parameters may be specific to the generator used, in part since initialization can as well be taken care of externally to the Pythia code.

An example illustrating how to run with an external random number generator is provided in main24.cc.