Blender V5.0
BLI_rand.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
12#include "BLI_span.hh"
13
14namespace blender {
15
17 private:
18 uint64_t x_;
19
20 public:
22 {
23 this->seed(seed);
24 }
25
31
35 void seed(uint32_t seed)
36 {
37 constexpr uint64_t lowseed = 0x330E;
38 x_ = (uint64_t(seed) << 16) | lowseed;
39 }
40
44 void seed_random(uint32_t seed);
45
46 uint32_t get_uint32()
47 {
48 this->step();
49 return uint32_t(x_ >> 17);
50 }
51
53 {
54 this->step();
55 return int32_t(x_ >> 17);
56 }
57
59 {
60 return (uint64_t(this->get_uint32()) << 32) | this->get_uint32();
61 }
62
66 int32_t get_int32(int32_t max_exclusive)
67 {
68 BLI_assert(max_exclusive > 0);
69 return this->get_int32() % max_exclusive;
70 }
71
75 double get_double()
76 {
77 return double(this->get_int32()) / 0x80000000;
78 }
79
83 float get_float()
84 {
85 return (float)this->get_int32() / 0x80000000;
86 }
87
88 template<typename T> void shuffle(MutableSpan<T> values)
89 {
90 /* Cannot shuffle arrays of this size yet. */
91 BLI_assert(values.size() <= INT32_MAX);
92
93 for (int i = values.size() - 1; i >= 2; i--) {
94 int j = this->get_int32(i);
95 if (i != j) {
96 std::swap(values[i], values[j]);
97 }
98 }
99 }
100
105 {
106 float rand1 = this->get_float();
107 float rand2 = this->get_float();
108
109 if (rand1 + rand2 > 1.0f) {
110 rand1 = 1.0f - rand1;
111 rand2 = 1.0f - rand2;
112 }
113
114 return float3(rand1, rand2, 1.0f - rand1 - rand2);
115 }
116
121 int round_probabilistic(float x);
122
130 void get_bytes(MutableSpan<char> r_bytes);
131
135 void skip(int64_t n)
136 {
137 while (n--) {
138 this->step();
139 }
140 }
141
142 private:
143 void step()
144 {
145 constexpr uint64_t multiplier = 0x5DEECE66Dll;
146 constexpr uint64_t addend = 0xB;
147 constexpr uint64_t mask = 0x0000FFFFFFFFFFFFll;
148
149 x_ = (multiplier * x_ + addend) & mask;
150 }
151};
152
153} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
ATTR_WARN_UNUSED_RESULT const BMVert * v2
long long int int64_t
unsigned long long int uint64_t
static unsigned long seed
Definition btSoftBody.h:39
constexpr int64_t size() const
Definition BLI_span.hh:493
void get_bytes(MutableSpan< char > r_bytes)
Definition rand.cc:373
int round_probabilistic(float x)
Definition rand.cc:306
RandomNumberGenerator(uint32_t seed=0)
Definition BLI_rand.hh:21
void seed_random(uint32_t seed)
Definition rand.cc:297
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition rand.cc:354
void shuffle(MutableSpan< T > values)
Definition BLI_rand.hh:88
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition rand.cc:335
void seed(uint32_t seed)
Definition BLI_rand.hh:35
int32_t get_int32(int32_t max_exclusive)
Definition BLI_rand.hh:66
static RandomNumberGenerator from_random_seed()
Definition rand.cc:288
#define INT32_MAX
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
VecBase< float, 2 > float2
VecBase< float, 3 > float3
i
Definition text_draw.cc:230