0026601: Visualization, Ray Tracing - make Path Tracing results reproducible for...
[occt.git] / src / math / math_BullardGenerator.hxx
1 // Created on: 2014-07-18
2 // Created by: Alexander Malyshev
3 // Copyright (c) 2014-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _math_BullardGenerator_HeaderFile
17 #define _math_BullardGenerator_HeaderFile
18
19 #include <Standard_Real.hxx>
20
21 //! Fast random number generator (the algorithm proposed by Ian C. Bullard).
22 class math_BullardGenerator
23 {
24 public:
25
26   //! Creates new Xorshift 64-bit RNG.
27   math_BullardGenerator (unsigned int theSeed = 1)
28   : myStateHi (theSeed)
29   {
30     SetSeed (theSeed);
31   }
32
33   //! Setup new seed / reset defaults.
34   void SetSeed (unsigned int theSeed = 1)
35   {
36     myStateHi = theSeed;
37     myStateLo = theSeed ^ 0x49616E42;
38   }
39
40   //! Generates new 64-bit integer value.
41   unsigned int NextInt()
42   {
43     myStateHi = (myStateHi >> 2) + (myStateHi << 2);
44
45     myStateHi += myStateLo;
46     myStateLo += myStateHi;
47
48     return myStateHi;
49   }
50
51   //! Generates new floating-point value.
52   Standard_Real NextReal()
53   {
54     return NextInt() / static_cast<Standard_Real> (0xFFFFFFFFu);
55   }
56
57 private:
58
59   unsigned int myStateHi;
60   unsigned int myStateLo;
61
62 };
63
64 #endif