AME
ame_Random.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include <bit>
13#include <cstdint>
14
15namespace
16{
17inline uint32_t s[4] = { 123, 234, 345, 97 }; //random seed
18inline constexpr double DOUBLE_UNIT = 0x1.0p-53; // 1.0f / (1 << 53)
19inline constexpr float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24)
20
21inline uint32_t next (void)
22{
23 const uint32_t result = s[0] + s[3];
24
25 const uint32_t t = s[1] << 9;
26
27 s[2] ^= s[0];
28 s[3] ^= s[1];
29 s[1] ^= s[2];
30 s[0] ^= s[3];
31
32 s[2] ^= t;
33
34 s[3] = std::rotl (s[3], 11);
35
36 return result;
37}
38} // namespace
39
40namespace ame
41{
47inline float random()
48{
49 return (next() >> 8) * FLOAT_UNIT;
50}
51
56inline float noise()
57{
58 return (next() >> 7) * FLOAT_UNIT - 1.0f; //[-1, 1]
59 /* もうひとつの方法. 負荷はほぼ同じ.
60 return (static_cast<int32_t>(next()) >> 8) * 0x1.0p-23f;//[-1, 1]
61 */
62}
63} // namespace ame
float noise()
White noise.
Definition: ame_Random.hpp:56
float random()
Random.
Definition: ame_Random.hpp:47