AkRng.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/SoundEngine/Common/AkTypes.h>
  22. /// A pseudorandom number generator appropriate for introducing randomness in DSP processing
  23. /// LCG with Newlib/Musl characteristics: 64-bit seed, 31-bit output (see http://en.wikipedia.org/wiki/Linear_congruential_generator)
  24. /// Warning: This RNG is not cryptographically secure! Do not use it as such!
  25. class CAkRng
  26. {
  27. public:
  28. static constexpr AkUInt64 RANDOM_A = 6364136223846793005ULL;
  29. static constexpr AkUInt64 RANDOM_C = 1;
  30. static constexpr AkUInt32 RANDOM_MAX = 0x7FFFFFFF; // 31 bits
  31. /// Initialize using the specified seed
  32. CAkRng(AkUInt64 uSeed)
  33. : m_uSeed(uSeed)
  34. {}
  35. /// Returns the current seed value of the RNG
  36. inline AkUInt64 Seed() const
  37. {
  38. return m_uSeed;
  39. }
  40. /// Returns the next random number to be generated without advancing the RNG state
  41. inline AkUInt32 Peek() const
  42. {
  43. return Peek(m_uSeed);
  44. }
  45. /// Returns a random 31-bit unsigned integer
  46. inline AkUInt32 Random()
  47. {
  48. return Random(m_uSeed);
  49. }
  50. /// Returns a random 31-bit integer
  51. inline AkInt32 RandomInt()
  52. {
  53. return RandomInt(m_uSeed);
  54. }
  55. /// Returns a random float from 0.0 to 1.0
  56. AkReal32 RandomFloat()
  57. {
  58. return (AkReal32)Random(m_uSeed) / (AkReal32)RANDOM_MAX;
  59. }
  60. /// Returns a random 31-bit unsigned integer using provided seed
  61. static inline AkInt32 Random(AkUInt64 &io_uSeed)
  62. {
  63. io_uSeed = io_uSeed * RANDOM_A + RANDOM_C;
  64. return Peek(io_uSeed);
  65. }
  66. /// Returns a random 31-bit integer using provided seed
  67. static inline AkInt32 RandomInt(AkUInt64 &io_uSeed)
  68. {
  69. io_uSeed = io_uSeed * RANDOM_A + RANDOM_C;
  70. return (AkInt32)Peek(io_uSeed);
  71. }
  72. /// Returns the next random number to be generated without advancing the RNG state
  73. static inline AkUInt32 Peek(AkUInt64 in_uSeed)
  74. {
  75. return (AkUInt32)(in_uSeed >> 33);
  76. }
  77. private:
  78. AkUInt64 m_uSeed;
  79. };