ddc
ddcRandom.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 // DDC originally by Alexey Sokirko
3 // Changes and modifications 2011-2015 by Bryan Jurish
4 //
5 // This file is part of DDC.
6 //
7 // DDC is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // DDC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
19 //
20 
21 #ifndef DDC_RANDOM_H
22 #define DDC_RANDOM_H
23 
24 #include <stdlib.h>
25 #include "ddcConfig.h"
26 
27 #if defined(HAVE_ERAND48) || defined(HAVE_JRAND48) || defined(HAVE_NRAND48)
28 # define DDC_RAND48 1
29 #else
30 # undef DDC_RAND48
31 #endif
32 
33 //======================================================================
38 class DDCRandom {
39 public:
41  unsigned int m_seed;
42 
43 #ifdef DDC_RAND48
44  unsigned short m_state[3];
46 #endif
47 
48 public:
49  //------------------------------------------------------------
51 
52  DDCRandom(unsigned int seed=0)
54  {
55  set_seed(seed);
56  };
57 
59  void set_seed(unsigned int seed=0)
60  {
61  m_seed = seed;
62 #ifdef DDC_RAND48
63  m_state[0]=seed;
64  m_state[1]=seed;
65  m_state[2]=seed;
66 #endif
67  };
68 
70  ~DDCRandom(void) {};
72 
73 public:
74  //------------------------------------------------------------
76 
77 
79  inline double random_d(void) {
80 #if defined(HAVE_ERAND48)
81  return erand48(m_state);
82 #elif defined(HAVE_RAND_R)
83  return (double)rand_r(&m_seed) / (double)RAND_MAX;
84 #else
85  srand(m_seed++);
86  return (double)rand() / (double)RAND_MAX;
87 #endif
88  };
89 
91  inline long random_l(void) {
92 #if defined(HAVE_JRAND48)
93  return (long)jrand48(m_state);
94 #elif defined(HAVE_RAND_R)
95  return (long)rand_r(&m_seed);
96 #else
97  srand(m_seed++);
98  return (long)rand();
99 #endif
100  };
101 
103  inline long random_n(void) {
104  //return (unsigned long)random_l();
105 #if defined(HAVE_NRAND48)
106  return (long)nrand48(m_state);
107 #else
108  return random_l();
109 #endif
110  };
112 };
113 
114 #endif /* DDC_RANDOM_H */
115 
116 /*--- emacs style variables ---
117  * Local Variables:
118  * mode: C++
119  * c-file-style: "ellemtel"
120  * c-basic-offset: 4
121  * tab-width: 8
122  * indent-tabs-mode: nil
123  * End:
124  */
long random_l(void)
returns a pseudo-random long in the range [-2**31,2**31)
Definition: ddcRandom.h:91
~DDCRandom(void)
Default destructor.
Definition: ddcRandom.h:70
unsigned int m_seed
underlying random seed for POSIX.1 random numbers using rand_r() or srand()+rand() hack ...
Definition: ddcRandom.h:41
long random_n(void)
returns a pseudo-random unsigned long in the range [0,2**31)
Definition: ddcRandom.h:103
unsigned short m_state[3]
underlying computation state (defaults to {m_seed,m_seed,m_seed}) for jrand48() & friends ...
Definition: ddcRandom.h:45
DDCRandom(unsigned int seed=0)
Constructor given state.
Definition: ddcRandom.h:53
double random_d(void)
returns a pseudo-random double in the range [0,1)
Definition: ddcRandom.h:79
void set_seed(unsigned int seed=0)
set internal seed
Definition: ddcRandom.h:59
thread-safe pseudo-random number stream using drand48() & friends
Definition: ddcRandom.h:38