1 // ---------------------------------------------------------------------- 2 // 3 // Copyright (C) 2003-2018 Fons Adriaensen <fons@linuxaudio.org> 4 // 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation; either version 2 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 // 19 // ---------------------------------------------------------------------- 20 21 22 #ifndef __RNGEN_H 23 #define __RNGEN_H 24 25 26 #include <stdint.h> 27 28 29 class Rngen 30 { 31 public: 32 33 Rngen (uint32_t s = 0); 34 35 void init (uint32_t s); 36 irand(void)37 uint32_t irand (void) 38 { 39 _x = 6364136223846793005 * _x + 1442695040888963407; 40 return _x >> 32; 41 } 42 urand(void)43 double urand (void) { return irand () / _p32d; } 44 double grand (void); 45 void grand (double *x, double *y); urandf(void)46 float urandf (void) { return irand () / _p32f; } 47 float grandf (void); 48 void grandf (float *x, float *y); 49 50 ~Rngen (void); 51 Rngen (const Rngen&); // disabled, not to be used 52 Rngen& operator=(const Rngen&); // disabled, not to be used 53 54 private: 55 56 int init_urandom (); 57 58 uint64_t _x; 59 bool _md; 60 bool _mf; 61 double _bd; 62 float _bf; 63 64 static const double _p31d; 65 static const double _p32d; 66 static const float _p31f; 67 static const float _p32f; 68 }; 69 70 71 #endif 72