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

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise