Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
random.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba 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, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_RANDOM_H_)
21 #define __MITSUBA_CORE_RANDOM_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <mitsuba/core/cobject.h>
25 
26 /*
27  SIMD oriented Fast Mersenne Twister (SFMT) pseudorandom number generator
28  http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/
29 
30  Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
31  University. All rights reserved.
32 
33  Redistribution and use in source and binary forms, with or without
34  modification, are permitted provided that the following conditions are
35  met:
36 
37  * Redistributions of source code must retain the above copyright
38  notice, this list of conditions and the following disclaimer.
39  * Redistributions in binary form must reproduce the above
40  copyright notice, this list of conditions and the following
41  disclaimer in the documentation and/or other materials provided
42  with the distribution.
43  * Neither the name of the Hiroshima University nor the names of
44  its contributors may be used to endorse or promote products
45  derived from this software without specific prior written
46  permission.
47 
48  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 
60  References:
61  M. Saito and M. Matsumoto,
62  ``SIMD-oriented Fast Mersenne Twister:
63  a 128-bit Pseudorandom Number Generator''
64  Monte Carlo and Quasi-Monte Carlo Method 2006.
65  Springer (2008) 607--622.
66  DOI: 10.1007/978-3-540-74496-2_36
67  T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
68  ACM Transactions on Modeling and
69  Computer Simulation 10. (2000) 348--357.
70  M. Matsumoto and T. Nishimura,
71  ``Mersenne Twister: a 623-dimensionally equidistributed
72  uniform pseudorandom number generator''
73  ACM Transactions on Modeling and
74  Computer Simulation 8. (Jan. 1998) 3--30.
75  * \ingroup libcore
76 */
77 
79 
80 /**
81  * \brief %Random number generator based on SIMD-oriented Fast Mersenne Twister
82  *
83  * \author Mutsuo Saito and Makoto Matsumoto at Hiroshima University.
84  *
85  * \ingroup libcore
86  * \ingroup libpython
87  */
89 public:
90  /**
91  * \brief Construct a new seeded random generator.
92  *
93  * Uses the default seed on Windows and '/dev/urandom'
94  * on OSX and Linux.
95  */
96  Random();
97 
98  /**
99  * \brief Construct a random generator with a custom seed
100  */
101  Random(uint64_t seed);
102 
103  /// Construct a new random generator seeded from a pre-existing one
104  Random(Random *random);
105 
106  /// Unserialize a random generator
107  Random(Stream *stream, InstanceManager *manager);
108 
109  /// Copy the state from another random generator
110  void set(Random *random);
111 
112  /// Seed the random generator with a single 64bit value
113  void seed(uint64_t value = 5489ULL);
114 
115  /// Seed the random generator from another random generator
116  void seed(Random *random);
117 
118  /**
119  * \brief Seed the random generator from an array
120  * \remark This function is currently not exposed
121  * by the Python bindings
122  */
123  void seed(uint64_t *values, uint64_t length);
124 
125  /// Return an integer on the [0, 2^63-1]-interval
126  uint64_t nextULong();
127 
128  /// Return an integer on the [0, n)-interval
129  uint32_t nextUInt(uint32_t n);
130 
131  /// Return an integer on the [0, n)-interval
132  size_t nextSize(size_t n);
133 
134  /// Return a floating point value on the [0, 1) interval
135  Float nextFloat();
136 
137  /// Return a normally distributed value
138  Float nextStandardNormal();
139 
140  /**
141  * \brief Draw a uniformly distributed permutation and permute the
142  * given STL container.
143  *
144  * See Knuth, TAoCP Vol. 2 (3rd 3d), Section 3.4.2.
145  *
146  * \remark This function is currently not exposed
147  * by the Python bindings
148  */
149  template <typename Iterator> void shuffle(Iterator it1, Iterator it2) {
150  for (Iterator it = it2 - 1; it > it1; --it)
151  std::iter_swap(it, it1 + nextSize((size_t) (it-it1)));
152  }
153 
154  /// Serialize a random generator to a binary data stream
155  void serialize(Stream *stream, InstanceManager *manager) const;
156 
158 protected:
159  /// Virtual destructor
160  virtual ~Random();
161 private:
162  struct State;
163  State *mt;
164 };
165 
166 
168 
169 #endif /* __MITSUBA_CORE_RANDOM_H_ */
Random number generator based on SIMD-oriented Fast Mersenne Twister
Definition: random.h:88
Base class of all reference-counted objects with serialization support.
Definition: serialization.h:35
virtual void serialize(Stream *stream, InstanceManager *manager) const =0
Serialize this object to a stream.
#define MTS_EXPORT_CORE
Definition: getopt.h:29
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
void shuffle(Iterator it1, Iterator it2)
Draw a uniformly distributed permutation and permute the given STL container.
Definition: random.h:149
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Coordinates the serialization and unserialization of object graphs.
Definition: serialization.h:65
#define MTS_NAMESPACE_END
Definition: platform.h:138