MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_random_xorshift.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_RANDOM_H_1626922980
12#define INCGUARD_MGL_RANDOM_H_1626922980
13
14#include <cstdint>
15
16namespace MGL::Random
17{
20{
21public:
22 /* ------------------------------------------------------------------------- */
27 /* ------------------------------------------------------------------------- */
28 explicit constexpr XorShift(uint32_t seed = 0) noexcept
29 : _seed(seed)
30 {}
31
32 /* ------------------------------------------------------------------------- */
37 /* ------------------------------------------------------------------------- */
38 constexpr uint32_t Get() noexcept
39 {
40 _seed = _seed ^ (_seed << 13);
41 _seed = _seed ^ (_seed >> 17);
42 _seed = _seed ^ (_seed << 5);
43
44 return _seed;
45 }
46
47 /* ------------------------------------------------------------------------- */
52 /* ------------------------------------------------------------------------- */
53 constexpr float GetFloat() noexcept
54 {
55 return static_cast<float>(static_cast<double>(Get()) / static_cast<double>(0xFFFFFFFF));
56 }
57
58 /* ------------------------------------------------------------------------- */
63 /* ------------------------------------------------------------------------- */
64 [[nodiscard]] constexpr uint32_t GetSeed() const noexcept
65 {
66 return _seed;
67 }
68
69 /* ------------------------------------------------------------------------- */
74 /* ------------------------------------------------------------------------- */
75 constexpr void SetSeed(uint32_t seed) noexcept
76 {
77 _seed = seed;
78 }
79
80private:
81 uint32_t _seed;
82};
83} // namespace MGL::Random
84
85#endif // INCGUARD_MGL_RANDOM_H_1626922980
86
87// vim: et ts=4 sw=4 sts=4
XorShift乱数ジェネレータ
Definition mgl_random_xorshift.h:20
constexpr XorShift(uint32_t seed=0) noexcept
コンストラクタ
Definition mgl_random_xorshift.h:28
constexpr uint32_t GetSeed() const noexcept
種の取得
Definition mgl_random_xorshift.h:64
constexpr float GetFloat() noexcept
乱数の取得(float版)
Definition mgl_random_xorshift.h:53
constexpr void SetSeed(uint32_t seed) noexcept
種の設定
Definition mgl_random_xorshift.h:75
constexpr uint32_t Get() noexcept
乱数の取得
Definition mgl_random_xorshift.h:38