Blender  V3.3
BLI_rand.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #pragma once
8 
9 #include "BLI_math.h"
10 #include "BLI_math_vec_types.hh"
11 #include "BLI_span.hh"
12 #include "BLI_utildefines.h"
13 
14 namespace blender {
15 
17  private:
18  uint64_t x_;
19 
20  public:
22  {
23  this->seed(seed);
24  }
25 
30  {
31  constexpr uint64_t lowseed = 0x330E;
32  x_ = (static_cast<uint64_t>(seed) << 16) | lowseed;
33  }
34 
39 
41  {
42  this->step();
43  return static_cast<uint32_t>(x_ >> 17);
44  }
45 
47  {
48  this->step();
49  return static_cast<int32_t>(x_ >> 17);
50  }
51 
55  int32_t get_int32(int32_t max_exclusive)
56  {
57  BLI_assert(max_exclusive > 0);
58  return this->get_int32() % max_exclusive;
59  }
60 
64  double get_double()
65  {
66  return (double)this->get_int32() / 0x80000000;
67  }
68 
72  float get_float()
73  {
74  return (float)this->get_int32() / 0x80000000;
75  }
76 
77  template<typename T> void shuffle(MutableSpan<T> values)
78  {
79  /* Cannot shuffle arrays of this size yet. */
80  BLI_assert(values.size() <= INT32_MAX);
81 
82  for (int i = values.size() - 1; i >= 2; i--) {
83  int j = this->get_int32(i);
84  if (i != j) {
85  std::swap(values[i], values[j]);
86  }
87  }
88  }
89 
94  {
95  float rand1 = this->get_float();
96  float rand2 = this->get_float();
97 
98  if (rand1 + rand2 > 1.0f) {
99  rand1 = 1.0f - rand1;
100  rand2 = 1.0f - rand2;
101  }
102 
103  return float3(rand1, rand2, 1.0f - rand1 - rand2);
104  }
105 
110  int round_probabilistic(float x);
111 
119  void get_bytes(MutableSpan<char> r_bytes);
120 
124  void skip(int64_t n)
125  {
126  while (n--) {
127  this->step();
128  }
129  }
130 
131  private:
132  void step()
133  {
134  constexpr uint64_t multiplier = 0x5DEECE66Dll;
135  constexpr uint64_t addend = 0xB;
136  constexpr uint64_t mask = 0x0000FFFFFFFFFFFFll;
137 
138  x_ = (multiplier * x_ + addend) & mask;
139  }
140 };
141 
142 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:46
void swap(T &a, T &b)
Definition: Common.h:19
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
ATTR_WARN_UNUSED_RESULT const BMVert * v2
constexpr int64_t size() const
Definition: BLI_span.hh:511
void get_bytes(MutableSpan< char > r_bytes)
Definition: rand.cc:444
int round_probabilistic(float x)
Definition: rand.cc:377
RandomNumberGenerator(uint32_t seed=0)
Definition: BLI_rand.hh:21
void seed_random(uint32_t seed)
Definition: rand.cc:368
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition: rand.cc:425
void shuffle(MutableSpan< T > values)
Definition: BLI_rand.hh:77
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition: rand.cc:406
void seed(uint32_t seed)
Definition: BLI_rand.hh:29
int32_t get_int32(int32_t max_exclusive)
Definition: BLI_rand.hh:55
const Mat x_
Definition: fundamental.cc:446
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
vec_base< 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