Blender V4.3
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
9#pragma once
10
12#include "BLI_span.hh"
13#include "BLI_utildefines.h"
14
15namespace blender {
16
18 private:
19 uint64_t x_;
20
21 public:
23 {
24 this->seed(seed);
25 }
26
32
37 {
38 constexpr uint64_t lowseed = 0x330E;
39 x_ = (uint64_t(seed) << 16) | lowseed;
40 }
41
46
48 {
49 this->step();
50 return uint32_t(x_ >> 17);
51 }
52
54 {
55 this->step();
56 return int32_t(x_ >> 17);
57 }
58
60 {
61 return (uint64_t(this->get_uint32()) << 32) | this->get_uint32();
62 }
63
67 int32_t get_int32(int32_t max_exclusive)
68 {
69 BLI_assert(max_exclusive > 0);
70 return this->get_int32() % max_exclusive;
71 }
72
76 double get_double()
77 {
78 return double(this->get_int32()) / 0x80000000;
79 }
80
84 float get_float()
85 {
86 return (float)this->get_int32() / 0x80000000;
87 }
88
89 template<typename T> void shuffle(MutableSpan<T> values)
90 {
91 /* Cannot shuffle arrays of this size yet. */
92 BLI_assert(values.size() <= INT32_MAX);
93
94 for (int i = values.size() - 1; i >= 2; i--) {
95 int j = this->get_int32(i);
96 if (i != j) {
97 std::swap(values[i], values[j]);
98 }
99 }
100 }
101
106 {
107 float rand1 = this->get_float();
108 float rand2 = this->get_float();
109
110 if (rand1 + rand2 > 1.0f) {
111 rand1 = 1.0f - rand1;
112 rand2 = 1.0f - rand2;
113 }
114
115 return float3(rand1, rand2, 1.0f - rand1 - rand2);
116 }
117
122 int round_probabilistic(float x);
123
131 void get_bytes(MutableSpan<char> r_bytes);
132
136 void skip(int64_t n)
137 {
138 while (n--) {
139 this->step();
140 }
141 }
142
143 private:
144 void step()
145 {
146 constexpr uint64_t multiplier = 0x5DEECE66Dll;
147 constexpr uint64_t addend = 0xB;
148 constexpr uint64_t mask = 0x0000FFFFFFFFFFFFll;
149
150 x_ = (multiplier * x_ + addend) & mask;
151 }
152};
153
154} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
typedef double(DMatrix)[4][4]
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static unsigned long seed
Definition btSoftBody.h:39
void get_bytes(MutableSpan< char > r_bytes)
Definition rand.cc:450
int round_probabilistic(float x)
Definition rand.cc:383
RandomNumberGenerator(uint32_t seed=0)
Definition BLI_rand.hh:22
void seed_random(uint32_t seed)
Definition rand.cc:374
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition rand.cc:431
void shuffle(MutableSpan< T > values)
Definition BLI_rand.hh:89
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition rand.cc:412
void seed(uint32_t seed)
Definition BLI_rand.hh:36
int32_t get_int32(int32_t max_exclusive)
Definition BLI_rand.hh:67
static RandomNumberGenerator from_random_seed()
Definition rand.cc:365
const Mat x_
VecBase< float, 3 > float3
#define INT32_MAX
Definition stdint.h:137
unsigned int uint32_t
Definition stdint.h:80
__int64 int64_t
Definition stdint.h:89
signed int int32_t
Definition stdint.h:77
unsigned __int64 uint64_t
Definition stdint.h:90