Computational Physics - Department of Physics

(Axel Boer) #1

11.6 Classes for Random Number Generators 375


Table 11.4Results as function of number of Monte Carlo samplesN. The exact answer isI≈ 93. 020 for the
integral. The suffixcrstands for the brute force approach whileisstands for the importance sampling results.
All calculations use ran0 as function to generate the uniform distribution.
N Icr Igd
10000 9.92072E+01 9.33225E+01
100000 8.75039E+01 9.30042E+01
1000000 9.56759E+01 9.29988E+01
10000000 9.15446E+01 9.30203E+01


11.6 Classes for Random Number Generators


We end this chapter with presenting a possible class for using random number genrerators.
The class consists of five files, one which defines the random number generators, random.h
and four separate files Ran0.h, Ran1.h, Ran2.h and Ran3.h discussed in the text. We list here
only the definitions contained in random.h. The file is well commented and all information is
contained within the file itself.


The file random.h
/*
@file Random.h
*@class Random



  • Interface for random number generators (RNG). The particular RNG are
    implemented in the various subclasses.


  • **/




#ifndefRANDOM_H
#defineRANDOM_H


classRandom{
protected:
longseed;
public:
/*
@brief Constructor.



  • @param seedA negative long integer. If none is given, seed takes the default value
    -1.
    **/
    Random(longseed
    =-1): seed(seed_){}
    //! Destructor
    virtual~Random();
    /**
    This function is useful in cases where it is necessary to takecare of the seed in
    order
    *to reproduce experiments with the same sequences.


  • *@return The seed used during the initialization of the Random Number Generator.
    **/
    longgetSeed()const{return seed;}
    //! Modify the seed.
    virtualreseed(longseed){seed = seed;}



Free download pdf