Sample SNPs
Fast ordered sampling of rows from large text or binary files. Special cases for DNA variant files (.bed, VCF, HapMap, etc).
random.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Anthony J. Greenberg
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7  *
8  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9  *
10  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11  *
12  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
14  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
15  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
16  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
17  * THE POSSIBILITY OF SUCH DAMAGE.
18  */
19 
21 
30 #ifndef random_hpp
31 #define random_hpp
32 
33 #include <cstdint>
34 
35 namespace sampFiles {
36  class RanDraw;
37  class Generate;
38  class GenerateHR;
39  class GenerateMT;
40 
45  class Generate {
46  protected:
48  Generate(){};
53  Generate(const Generate &old){};
58  Generate(Generate &&old){};
63  Generate & operator= (const Generate &old) = default;
68  Generate & operator= (Generate &&old) = default;
69 
70  public:
72  virtual ~Generate(){};
77  virtual volatile uint64_t ranInt() = 0;
78  };
79 
85  class GenerateHR : public Generate {
86  protected:
87  // no protected members
88  public:
97  GenerateHR(const GenerateHR &old){};
107  GenerateHR & operator= (const GenerateHR &old) = default;
112  GenerateHR & operator= (GenerateHR &&old) = default;
113 
120  volatile uint64_t ranInt();
121  };
122 
127  class GenerateMT : public Generate {
128  protected:
130  static const unsigned short _n;
132  static const unsigned short _m;
134  static const uint64_t _um;
136  static const uint64_t _lm;
138  static const uint64_t _b;
140  static const uint64_t _c;
142  static const uint64_t _d;
144  static const unsigned int _l;
146  static const unsigned int _s;
148  static const unsigned int _t;
150  static const unsigned int _u;
152  static const uint64_t _alt[2];
154  uint64_t _mt[312];
156  size_t _mti;
158  uint64_t _x;
159 
160  public:
165  GenerateMT();
172  GenerateMT(const GenerateMT &old) = default;
177  GenerateMT(GenerateMT &&old) = default;
182  GenerateMT & operator= (const GenerateMT &old) = default;
187  GenerateMT & operator= (GenerateMT &&old) = default;
188 
194  volatile uint64_t ranInt();
195  };
196 
202  class RanDraw {
203  private:
204  Generate *_rng;
205 
206  public:
212  RanDraw();
213 
215  ~RanDraw(){ delete _rng; };
220  RanDraw(const RanDraw &old) = default;
225  RanDraw(RanDraw &&old) = default;
226 
231  RanDraw & operator= (const RanDraw &old) = default;
236  RanDraw & operator= (RanDraw &&old) = default;
237 
242  volatile uint64_t ranInt(){ return _rng->ranInt(); };
243 
248  volatile double runif() {return 5.42101086242752217E-20*static_cast<double>(_rng->ranInt()); };
253  volatile double runifnz();
264  volatile uint64_t vitterA(const double &n, const double &N);
275  volatile uint64_t vitter(const double &n, const double &N);
276  };
277 
278 }
279 
280 #endif /* random_hpp */
sampFiles::GenerateHR
Hardware random number generating class.
Definition: random.hpp:85
sampFiles::GenerateMT::_d
static const uint64_t _d
Tempering bitmask.
Definition: random.hpp:142
sampFiles::GenerateMT
Pseudo-random number generator.
Definition: random.hpp:127
sampFiles::GenerateHR::GenerateHR
GenerateHR(GenerateHR &&old)
Move constructor.
Definition: random.hpp:102
sampFiles::Generate::~Generate
virtual ~Generate()
Protected destructor.
Definition: random.hpp:72
sampFiles::RanDraw::runif
volatile double runif()
Generate a uniform deviate.
Definition: random.hpp:248
sampFiles::RanDraw::vitter
volatile uint64_t vitter(const double &n, const double &N)
Sample from Vitter's distribution, method D.
Definition: random.cpp:218
sampFiles::GenerateMT::_l
static const unsigned int _l
Tempering shift.
Definition: random.hpp:144
sampFiles::RanDraw::ranInt
volatile uint64_t ranInt()
Generate random integer.
Definition: random.hpp:242
sampFiles::GenerateMT::GenerateMT
GenerateMT()
Default constructor.
Definition: random.cpp:85
sampFiles::GenerateMT::_n
static const unsigned short _n
Degree of recurrence.
Definition: random.hpp:130
sampFiles::RanDraw::RanDraw
RanDraw(const RanDraw &old)=default
Copy constructor.
sampFiles::GenerateMT::operator=
GenerateMT & operator=(const GenerateMT &old)=default
Copy assignment operator.
sampFiles::GenerateMT::GenerateMT
GenerateMT(const GenerateMT &old)=default
Copy constructor.
sampFiles::GenerateMT::~GenerateMT
~GenerateMT()
Protected destructor.
Definition: random.hpp:167
sampFiles::GenerateMT::_c
static const uint64_t _c
Tempering bitmask.
Definition: random.hpp:140
sampFiles::RanDraw::vitterA
volatile uint64_t vitterA(const double &n, const double &N)
Sample from Vitter's distribution, method A.
Definition: random.cpp:184
sampFiles::GenerateMT::ranInt
volatile uint64_t ranInt()
Generate a pseudo-random 64-bit unsigned integer.
Definition: random.cpp:104
sampFiles::GenerateHR::~GenerateHR
~GenerateHR()
Destructor.
Definition: random.hpp:92
sampFiles::Generate::operator=
Generate & operator=(const Generate &old)=default
Protected copy assignment operator.
sampFiles::GenerateMT::_mti
size_t _mti
State of the array index.
Definition: random.hpp:156
sampFiles::GenerateMT::_um
static const uint64_t _um
Most significant 33 bits.
Definition: random.hpp:134
sampFiles::RanDraw
Random number generating class.
Definition: random.hpp:202
sampFiles::Generate::ranInt
virtual volatile uint64_t ranInt()=0
Generate a (pseudo-)random 64-bit unsigned integer.
sampFiles::RanDraw::RanDraw
RanDraw(RanDraw &&old)=default
Move constructor.
sampFiles::GenerateHR::GenerateHR
GenerateHR(const GenerateHR &old)
Copy constructor.
Definition: random.hpp:97
sampFiles::GenerateMT::_lm
static const uint64_t _lm
Least significant 31 bits.
Definition: random.hpp:136
sampFiles::GenerateHR::ranInt
volatile uint64_t ranInt()
Generate a random 64-bit unsigned integer.
Definition: random.cpp:42
sampFiles::GenerateMT::_b
static const uint64_t _b
Tempering bitmask.
Definition: random.hpp:138
sampFiles::RanDraw::RanDraw
RanDraw()
Default constructor.
Definition: random.cpp:134
sampFiles::GenerateHR::operator=
GenerateHR & operator=(const GenerateHR &old)=default
Copy assignment operator.
sampFiles::RanDraw::operator=
RanDraw & operator=(const RanDraw &old)=default
Copy assignment.
sampFiles::GenerateMT::GenerateMT
GenerateMT(GenerateMT &&old)=default
Move constructor.
sampFiles::RanDraw::~RanDraw
~RanDraw()
Destructor.
Definition: random.hpp:215
sampFiles::Generate::Generate
Generate()
Protected default constructor.
Definition: random.hpp:48
sampFiles::Generate
Abstract base random number class.
Definition: random.hpp:45
sampFiles::GenerateMT::_x
uint64_t _x
Current state.
Definition: random.hpp:158
sampFiles::RanDraw::runifnz
volatile double runifnz()
Generate a non-zero uniform deviate.
Definition: random.cpp:175
sampFiles::GenerateMT::_u
static const unsigned int _u
Tempering shift.
Definition: random.hpp:150
sampFiles::GenerateMT::_t
static const unsigned int _t
Tempering shift.
Definition: random.hpp:148
sampFiles::GenerateMT::_alt
static const uint64_t _alt[2]
Array of alternative values for the twist.
Definition: random.hpp:152
sampFiles::GenerateHR::GenerateHR
GenerateHR()
Default constructor.
Definition: random.hpp:90
sampFiles::GenerateMT::_s
static const unsigned int _s
Tempering shift.
Definition: random.hpp:146
sampFiles::GenerateMT::_m
static const unsigned short _m
Middle word.
Definition: random.hpp:132
sampFiles::Generate::Generate
Generate(const Generate &old)
Protected copy constructor.
Definition: random.hpp:53
sampFiles::Generate::Generate
Generate(Generate &&old)
Protected move constructor.
Definition: random.hpp:58
sampFiles::GenerateMT::_mt
uint64_t _mt[312]
Generator state array.
Definition: random.hpp:154