Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:11:10

0001 //
0002 // -*- C++ -*-
0003 //
0004 // -----------------------------------------------------------------------
0005 //                             HEP Random
0006 //                       --- RanluxppEngine ---
0007 //                         class header file
0008 // -----------------------------------------------------------------------
0009 // Implementation of the RANLUX++ generator
0010 //
0011 // RANLUX++ is an LCG equivalent of RANLUX using 576 bit numbers.
0012 //
0013 // References:
0014 // A. Sibidanov
0015 //   A revision of the subtract-with-borrow random numbergenerators
0016 //   Computer Physics Communications, 221(2017), 299-303
0017 //
0018 // J. Hahnfeld, L. Moneta
0019 //   A Portable Implementation of RANLUX++
0020 //   vCHEP2021
0021 
0022 #ifndef RanluxppEngine_h
0023 #define RanluxppEngine_h
0024 
0025 #include "CLHEP/Random/RandomEngine.h"
0026 
0027 #include <cstdint>
0028 
0029 namespace CLHEP {
0030 
0031 /**
0032  * @author Jonas Hahnfeld
0033  * @ingroup random
0034  */
0035 class RanluxppEngine final : public HepRandomEngine {
0036 
0037 public:
0038   RanluxppEngine();
0039   RanluxppEngine(long seed);
0040   RanluxppEngine(std::istream &is);
0041   virtual ~RanluxppEngine();
0042   // Constructors and destructor
0043 
0044   double flat() override;
0045   // It returns a pseudo random number between 0 and 1,
0046   // excluding the end points.
0047 
0048   void flatArray(const int size, double *vect) override;
0049   // Fills the array "vect" of specified size with flat random values.
0050 
0051   void setSeed(long seed, int dummy = 0) override;
0052   // Sets the state of the algorithm according to seed.
0053 
0054   void setSeeds(const long *seeds, int dummy = 0) override;
0055   // Sets the state of the algorithm according to the zero terminated
0056   // array of seeds.  Only the first seed is used.
0057 
0058   void skip(uint64_t n);
0059   // Skip `n` random numbers without generating them.
0060 
0061   void saveStatus(const char filename[] = "Ranluxpp.conf") const override;
0062   // Saves in named file the current engine status.
0063 
0064   void restoreStatus(const char filename[] = "Ranluxpp.conf") override;
0065   // Reads from named file the last saved engine status and restores it.
0066 
0067   void showStatus() const override;
0068   // Dumps the engine status on the screen.
0069 
0070   std::string name() const override;
0071 
0072   // Optional methods to serialize the engine's state into vectors and streams.
0073   static std::string engineName();
0074   static std::string beginTag();
0075 
0076   std::ostream &put(std::ostream &os) const override;
0077   std::istream &get(std::istream &is) override;
0078 
0079   std::istream &getState(std::istream &is) override;
0080 
0081   std::vector<unsigned long> put() const override;
0082   bool get(const std::vector<unsigned long> &v) override;
0083   bool getState(const std::vector<unsigned long> &v) override;
0084 
0085   // Save and restore to/from streams
0086   operator double() override { return flat(); }
0087   operator float() override { return float(flat()); }
0088   operator unsigned int() override { return (unsigned int)nextRandomBits(); }
0089 
0090   // 1 value for the engine ID, 2 * 9 values for the state, and 2 more values
0091   // for the carry bit and the position.
0092   static const unsigned int VECTOR_STATE_SIZE = 21;
0093 
0094 private:
0095   void advance();
0096   uint64_t nextRandomBits();
0097 
0098   uint64_t fState[9]; ///< RANLUX state of the generator
0099   unsigned fCarry;    ///< Carry bit of the RANLUX state
0100   int fPosition = 0;  ///< Current position in bits
0101 
0102 }; // RanluxppEngine
0103 
0104 } // namespace CLHEP
0105 
0106 #endif