28.6 Random Elements

  • Random( C ) O
  • Random( list ) O

    Random returns a (pseudo-)random element of the collection C respectively the list list.

    The distribution of elements returned by Random depends on the argument. For a list list, all elements are equally likely. The same holds usually for finite collections C that are not lists. For infinite collections C some reasonable distribution is used.

    See the chapters of the various collections to find out which distribution is being used.

    For some collections ensuring a reasonable distribution can be difficult and require substantial runtime. If speed at the cost of equal distribution is desired, the operation PseudoRandom should be used instead.

    Note that Random is of course not an attribute.

    gap> Random(Rationals);
    -4
    gap> g:= Group( (1,2,3) );;  Random( g );  Random( g );
    ()
    (1,2,3)
    

  • PseudoRandom( C ) O
  • PseudoRandom( list ) O

    PseudoRandom returns a pseudo random element of the collection C respectively the list list, which can be roughly described as follows. For a list list, PseudoRandom returns the same as Random. For collections C that are not lists, the elements returned by PseudoRandom are not necessarily equally distributed, even for finite collections C; the idea is that Random (see Random) returns elements according to a reasonable distribution, PseudoRandom returns elements that are cheap to compute but need not satisfy this strong condition, and Representative (see Representative) returns arbitrary elements, probably the same element for each call.

    The method used by GAP to obtain random elements may depend on the type object.

    Many random methods in the library are eventually based on the function RandomList. As RandomList is restricted to lists of up to 228 elements, this may create problems for very large collections. Also note that the method used by RandomList is intended to provide a fast algorithm rather than to produce high quality randomness for statistical purposes.

    If you implement your own Random methods we recommend that they initialize their seed to a defined value when they are loaded to permit to reproduce calculations even if they involved random elements.

  • RandomList( list ) F

    returns a (pseudo-)random element with equal distribution from a list list of up to 228 elements.

    The algorithm used is an additive number generator (Algorithm A in section 3.2.2 of TACP2 with lag 30)

    This random number generator is (deliberatly) initialized to the same values when GAP is started, so different runs of GAP with the same input will always produce the same result, even if random calculations are involved.

    The random seed of this generator is stored in the two global variables R_N and R_X, whose values can be copied and assigned to, to put the generator back in a defined state.

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000