Blender  V2.93
BLI_rand.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #pragma once
22 
23 #include "BLI_float2.hh"
24 #include "BLI_float3.hh"
25 #include "BLI_math.h"
26 #include "BLI_span.hh"
27 #include "BLI_utildefines.h"
28 
29 namespace blender {
30 
32  private:
33  uint64_t x_;
34 
35  public:
37  {
38  this->seed(seed);
39  }
40 
45  {
46  constexpr uint64_t lowseed = 0x330E;
47  x_ = (static_cast<uint64_t>(seed) << 16) | lowseed;
48  }
49 
51 
53  {
54  this->step();
55  return static_cast<uint32_t>(x_ >> 17);
56  }
57 
59  {
60  this->step();
61  return static_cast<int32_t>(x_ >> 17);
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  void get_bytes(MutableSpan<char> r_bytes);
123 
127  void skip(int64_t n)
128  {
129  while (n--) {
130  this->step();
131  }
132  }
133 
134  private:
135  void step()
136  {
137  constexpr uint64_t multiplier = 0x5DEECE66Dll;
138  constexpr uint64_t addend = 0xB;
139  constexpr uint64_t mask = 0x0000FFFFFFFFFFFFll;
140 
141  x_ = (multiplier * x_ + addend) & mask;
142  }
143 };
144 
145 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
void swap(T &a, T &b)
Definition: Common.h:33
_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:524
void get_bytes(MutableSpan< char > r_bytes)
Definition: rand.cc:453
RandomNumberGenerator(uint32_t seed=0)
Definition: BLI_rand.hh:36
void seed_random(uint32_t seed)
Definition: rand.cc:383
float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
Definition: rand.cc:434
void shuffle(MutableSpan< T > values)
Definition: BLI_rand.hh:89
float2 get_triangle_sample(float2 v1, float2 v2, float2 v3)
Definition: rand.cc:415
void seed(uint32_t seed)
Definition: BLI_rand.hh:44
int32_t get_int32(int32_t max_exclusive)
Definition: BLI_rand.hh:67
const Mat x_
Definition: fundamental.cc:446
#define INT32_MAX
Definition: stdint.h:140
unsigned int uint32_t
Definition: stdint.h:83
__int64 int64_t
Definition: stdint.h:92
signed int int32_t
Definition: stdint.h:80
unsigned __int64 uint64_t
Definition: stdint.h:93
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)