Diligent Engine  v.2.4.g
FastRand.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
31 
32 namespace Diligent
33 {
34 
38 class FastRand
39 {
40 public:
41  using StateType = unsigned int;
42 
43  static constexpr StateType Max = 0x7FFF;
44 
45  explicit FastRand(StateType Seed) noexcept :
46  State{Seed}
47  {
48  }
49 
51  {
52  State = StateType{214013} * State + StateType{2531011};
53  return (State >> StateType{16}) & Max;
54  }
55 
56 private:
57  StateType State;
58 };
59 
61 template <typename Type>
62 class FastRandReal : private FastRand
63 {
64 public:
65  explicit FastRandReal(FastRand::StateType Seed) noexcept :
66  FastRand{Seed}
67  {}
68 
69  FastRandReal(FastRand::StateType Seed, Type _Min, Type _Max) noexcept :
70  FastRand{Seed},
71  Min{_Min},
72  Range{_Max - _Min}
73  {}
74 
76  {
77  return (static_cast<Type>(FastRand::operator()()) / static_cast<Type>(FastRand::Max)) * Range + Min;
78  }
79 
80 private:
81  const Type Min = 0.f;
82  const Type Range = 1.f;
83 };
84 
87 
89 class FastRandInt : private FastRand
90 {
91 public:
92  FastRandInt(FastRand::StateType Seed, int _Min, int _Max) noexcept :
93  FastRand{Seed},
94  Min{_Min},
95  Range{_Max - _Min + 1}
96  {
97  VERIFY_EXPR(_Max > _Min);
98  VERIFY(Range <= static_cast<int>(FastRand::Max), "Range is too large");
99  }
100 
102  {
103  return Min + static_cast<int>(FastRand::operator()()) % Range;
104  }
105 
106 private:
107  const int Min;
108  const int Range;
109 };
110 
111 } // namespace Diligent
Diligent::FastRand::Max
static constexpr StateType Max
Definition: FastRand.hpp:43
Diligent::FastRand
Fast random number generator.
Definition: FastRand.hpp:38
Diligent::FastRandReal::FastRandReal
FastRandReal(FastRand::StateType Seed) noexcept
Definition: FastRand.hpp:65
Diligent::FastRand::FastRand
FastRand(StateType Seed) noexcept
Definition: FastRand.hpp:45
Diligent::FastRandInt::FastRandInt
FastRandInt(FastRand::StateType Seed, int _Min, int _Max) noexcept
Definition: FastRand.hpp:92
Type
const D3D12_PIPELINE_STATE_SUBOBJECT_TYPE Type
Definition: PipelineStateD3D12Impl.cpp:69
Diligent::FastRandInt::operator()
int operator()()
Definition: FastRand.hpp:101
Diligent::FastRandReal::operator()
Type operator()()
Definition: FastRand.hpp:75
Diligent::FastRand::operator()
StateType operator()()
Definition: FastRand.hpp:50
Diligent::FastRandReal
Generates a real random number in [Min, Max] range.
Definition: FastRand.hpp:62
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::FastRandInt
Generates an integer random number in [Min, Max] range.
Definition: FastRand.hpp:89
Diligent::FastRand::StateType
unsigned int StateType
Definition: FastRand.hpp:41
Diligent::FastRandReal::FastRandReal
FastRandReal(FastRand::StateType Seed, Type _Min, Type _Max) noexcept
Definition: FastRand.hpp:69
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37