Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2023 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_tools_Random_h 23 : #define __PLUMED_tools_Random_h 24 : 25 : #include <string> 26 : #include <vector> 27 : #include <iosfwd> 28 : 29 : namespace PLMD { 30 : 31 : /// \ingroup TOOLBOX 32 812727 : class Random { 33 : static constexpr int IA=16807; 34 : static constexpr int IM=2147483647; 35 : static constexpr int IQ=127773; 36 : static constexpr int IR=2836; 37 : static constexpr int NTAB=32; 38 : static constexpr int NDIV=(1+(IM-1)/NTAB); 39 : static constexpr double fact=5.9604644775390625e-8; /* 1 / 2^24 */ 40 : static constexpr double EPS=3.0e-16; 41 : static constexpr double AM=1.0/IM; 42 : static constexpr double RNMX=1.0-EPS; 43 : static const std::string noname; 44 : bool incPrec; 45 : bool switchGaussian; 46 : double saveGaussian; 47 : int iy; 48 : int iv[NTAB]; 49 : int idum; 50 : std::string name; 51 : public: 52 : explicit Random(const std::string & title=noname); 53 : void setSeed(int idum); 54 : double RandU01(); 55 : double U01(); 56 : double U01d(); 57 : int RandInt(int i); 58 : void Shuffle(std::vector<unsigned>& vec); 59 : void WriteStateFull(std::ostream &)const; 60 : void ReadStateFull (std::istream &); 61 : void fromString(const std::string & str); 62 : void toString(std::string & str)const; 63 : friend std::ostream & operator<<(std::ostream & out,const Random & rng) { 64 : rng.WriteStateFull(out); 65 : return out; 66 : } 67 : friend std::istream & operator>>(std::istream & in,Random & rng) { 68 : rng.ReadStateFull(in); 69 : return in; 70 : } 71 : double Gaussian(); 72 : void IncreasedPrecis(bool i) { 73 : incPrec=i; 74 : } 75 : }; 76 : 77 : } 78 : 79 : #endif 80 :