Muesli
 All Classes Namespaces Files Functions Typedefs Enumerations
rng.h
1 /*
2  * rng.h
3  *
4  * Author: Steffen Ernsting <s.ernsting@uni-muenster.de>
5  *
6  * -------------------------------------------------------------------------------
7  *
8  * The MIT License
9  *
10  * Copyright 2014 Steffen Ernsting <s.ernsting@uni-muenster.de>,
11  * Herbert Kuchen <kuchen@uni-muenster.de.
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this software and associated documentation files (the "Software"), to deal
15  * in the Software without restriction, including without limitation the rights
16  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  * copies of the Software, and to permit persons to whom the Software is
18  * furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29  * THE SOFTWARE.
30  *
31  */
32 
33 #pragma once
34 
35 #include "muesli.h"
36 #ifdef __CUDACC__
37 #include <thrust/random.h>
38 #endif
39 #include <random>
40 
41 
42 namespace msl {
43 
50 class Rng
51 {
52 public:
56  MSL_USERFUNC
57  Rng()
58  : min(1.f), max(0.f), rng(hash(msl::getUniqueID())), dist(0.f, 1.f)
59  {
60  }
61 
69  MSL_USERFUNC
70  Rng(float minVal, float maxVal)
71  : min(minVal), max(maxVal), rng(hash(msl::getUniqueID())), dist(minVal, maxVal)
72  {
73  }
74 
83  MSL_USERFUNC
84  Rng(float minVal, float maxVal, size_t seed)
85  : min(minVal), max(maxVal), rng(seed), dist(minVal, maxVal)
86  {
87  }
88 
94  MSL_USERFUNC
95  float operator() ()
96  {
97  return dist(rng);
98  }
99 
100 private:
101  float min, max;
102 #ifdef __CUDA_ARCH__
103  thrust::default_random_engine rng;
104  thrust::uniform_real_distribution<float> dist;
105 #else
106  std::default_random_engine rng;
107  std::uniform_real_distribution<float> dist;
108 #endif
109 
110  MSL_USERFUNC
111  size_t hash(size_t a) const
112  {
113  a = (a + 0x7ed55d16) + (a << 12);
114  a = (a ^ 0xc761c23c) ^ (a >> 19);
115  a = (a + 0x165667b1) + (a << 5);
116  a = (a + 0xd3a2646c) ^ (a << 9);
117  a = (a + 0xfd7046c5) + (a << 3);
118  a = (a ^ 0xb55a4f09) ^ (a >> 16);
119  return a;
120  }
121 };
122 
123 } // namespace msl
124 
125 
MSL_USERFUNC size_t getUniqueID()
Returns a unique thread id.
Definition: muesli.h:278
MSL_USERFUNC Rng()
Default constructor.
Definition: rng.h:57
MSL_USERFUNC Rng(float minVal, float maxVal, size_t seed)
Creates a pseudo random number generator with minimum value minVal and maximum value maxVal and sets ...
Definition: rng.h:84
Class Rng represents a pseudo random number generator that can be called by both the CPU and the GPU...
Definition: rng.h:50
MSL_USERFUNC Rng(float minVal, float maxVal)
Creates a pseudo random number generator with minimum value minVal and maximum value maxVal...
Definition: rng.h:70
MSL_USERFUNC float operator()()
Definition: rng.h:95
Contains global definitions such as macros, functions, enums and classes, and constants in order to c...