Blender V4.3
rand.cc File Reference
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include "MEM_guardedalloc.h"
#include "BLI_bitmap.h"
#include "BLI_compiler_compat.h"
#include "BLI_math_vector.h"
#include "BLI_rand.h"
#include "BLI_rand.hh"
#include "BLI_sys_types.h"
#include "BLI_threads.h"
#include "BLI_strict_flags.h"

Go to the source code of this file.

Classes

struct  RNG
 
struct  RNG_THREAD_ARRAY
 

Namespaces

namespace  blender
 

Macros

#define hash   BLI_noise_hash_uchar_512
 

Functions

RNGBLI_rng_new (uint seed)
 
RNGBLI_rng_new_srandom (uint seed)
 
RNGBLI_rng_copy (const RNG *rng)
 
void BLI_rng_free (RNG *rng)
 
void BLI_rng_seed (RNG *rng, uint seed)
 
void BLI_rng_srandom (RNG *rng, uint seed)
 
void BLI_rng_get_char_n (RNG *rng, char *bytes, size_t bytes_len)
 
int BLI_rng_get_int (RNG *rng)
 
uint BLI_rng_get_uint (RNG *rng)
 
double BLI_rng_get_double (RNG *rng)
 
float BLI_rng_get_float (RNG *rng)
 
void BLI_rng_get_float_unit_v2 (RNG *rng, float v[2])
 
void BLI_rng_get_float_unit_v3 (RNG *rng, float v[3])
 
void BLI_rng_get_tri_sample_float_v2 (RNG *rng, const float v1[2], const float v2[2], const float v3[2], float r_pt[2])
 
void BLI_rng_get_tri_sample_float_v3 (RNG *rng, const float v1[3], const float v2[3], const float v3[3], float r_pt[3])
 
void BLI_rng_shuffle_array (RNG *rng, void *data, uint elem_size_i, uint elem_num)
 
void BLI_rng_shuffle_bitmap (RNG *rng, BLI_bitmap *bitmap, uint bits_num)
 
void BLI_rng_skip (RNG *rng, int n)
 
void BLI_array_frand (float *ar, int count, uint seed)
 
float BLI_hash_frand (uint seed)
 
void BLI_array_randomize (void *data, uint elem_size, uint elem_num, uint seed)
 
void BLI_bitmap_randomize (BLI_bitmap *bitmap, uint bits_num, uint seed)
 
void BLI_thread_srandom (int thread, uint seed)
 
int BLI_thread_rand (int thread)
 
float BLI_thread_frand (int thread)
 
RNG_THREAD_ARRAYBLI_rng_threaded_new ()
 
void BLI_rng_threaded_free (RNG_THREAD_ARRAY *rngarr)
 
int BLI_rng_thread_rand (RNG_THREAD_ARRAY *rngarr, int thread)
 
BLI_INLINE double halton_ex (double invprimes, double *offset)
 
void BLI_halton_1d (uint prime, double offset, int n, double *r)
 
void BLI_halton_2d (const uint prime[2], double offset[2], int n, double *r)
 
void BLI_halton_3d (const uint prime[3], double offset[3], int n, double *r)
 
void BLI_halton_2d_sequence (const uint prime[2], double offset[2], int n, double *r)
 
BLI_INLINE double radical_inverse (uint n)
 
void BLI_hammersley_1d (uint n, double *r)
 
void BLI_hammersley_2d_sequence (uint n, double *r)
 

Variables

uchar BLI_noise_hash_uchar_512 [512]
 
static RNG rng_tab [BLENDER_MAX_THREADS]
 

Macro Definition Documentation

◆ hash

Definition at line 28 of file rand.cc.

Referenced by BLI_thread_srandom().

Function Documentation

◆ BLI_array_frand()

void BLI_array_frand ( float * ar,
int count,
unsigned int seed )

Fill an array with random numbers.

Definition at line 169 of file rand.cc.

References BLI_rng_get_float(), BLI_rng_srandom(), count, and seed.

◆ BLI_array_randomize()

void BLI_array_randomize ( void * data,
unsigned int elem_size,
unsigned int elem_num,
unsigned int seed )

Shuffle an array randomly using the given seed contents. This routine does not use nor modify the state of the BLI random number generator.

Definition at line 188 of file rand.cc.

References BLI_rng_seed(), BLI_rng_shuffle_array(), and seed.

Referenced by edbm_select_random_exec(), lattice_select_random_exec(), modify_mesh(), blender::ed::object::object_select_random_exec(), psys_thread_context_init_distribute(), random_heap_helper(), random_heap_reinsert_helper(), random_heapsimple_helper(), and testbuffer_list_data_randomize().

◆ BLI_bitmap_randomize()

void BLI_bitmap_randomize ( BLI_bitmap * bitmap,
uint bits_num,
uint seed )

Definition at line 196 of file rand.cc.

References BLI_rng_seed(), BLI_rng_shuffle_bitmap(), and seed.

Referenced by curve_select_random_exec().

◆ BLI_halton_1d()

void BLI_halton_1d ( unsigned int prime,
double offset,
int n,
double * r )

Return the _n_th number of the given low-discrepancy sequence.

Definition at line 284 of file rand.cc.

References double(), and halton_ex().

◆ BLI_halton_2d()

void BLI_halton_2d ( const uint prime[2],
double offset[2],
int n,
double * r )

Definition at line 295 of file rand.cc.

References double(), and halton_ex().

Referenced by blender::deform_drawing(), and blender::eevee::Sampling::step().

◆ BLI_halton_2d_sequence()

void BLI_halton_2d_sequence ( const unsigned int prime[2],
double offset[2],
int n,
double * r )

Return the whole low-discrepancy sequence up to n.

Definition at line 322 of file rand.cc.

References double(), and halton_ex().

◆ BLI_halton_3d()

void BLI_halton_3d ( const uint prime[3],
double offset[3],
int n,
double * r )

◆ BLI_hammersley_1d()

void BLI_hammersley_1d ( uint n,
double * r )

Definition at line 350 of file rand.cc.

References radical_inverse().

Referenced by hammersley_create().

◆ BLI_hammersley_2d_sequence()

void BLI_hammersley_2d_sequence ( uint n,
double * r )

Definition at line 355 of file rand.cc.

References double(), and radical_inverse().

◆ BLI_hash_frand()

float BLI_hash_frand ( unsigned int seed)

Return a pseudo-random (hash) float from an integer value

Definition at line 180 of file rand.cc.

References BLI_rng_get_float(), BLI_rng_srandom(), and seed.

Referenced by modify_mesh().

◆ BLI_rng_copy()

RNG * BLI_rng_copy ( const RNG * rng)

Definition at line 53 of file rand.cc.

◆ BLI_rng_free()

◆ BLI_rng_get_char_n()

void BLI_rng_get_char_n ( RNG * rng,
char * bytes,
size_t bytes_len )

Definition at line 73 of file rand.cc.

References blender::RandomNumberGenerator::get_bytes(), and RNG::rng.

◆ BLI_rng_get_double()

double BLI_rng_get_double ( struct RNG * rng)
Returns
Random value (0..1), but never 1.0.

Definition at line 88 of file rand.cc.

References blender::RandomNumberGenerator::get_double(), and RNG::rng.

Referenced by BLI_jitter_init(), hammersley_create(), and particles_fluid_step().

◆ BLI_rng_get_float()

◆ BLI_rng_get_float_unit_v2()

void BLI_rng_get_float_unit_v2 ( RNG * rng,
float v[2] )

Definition at line 98 of file rand.cc.

References copy_v2_v2(), blender::RandomNumberGenerator::get_unit_float2(), RNG::rng, and v.

◆ BLI_rng_get_float_unit_v3()

void BLI_rng_get_float_unit_v3 ( RNG * rng,
float v[3] )

Definition at line 103 of file rand.cc.

References copy_v3_v3(), blender::RandomNumberGenerator::get_unit_float3(), RNG::rng, and v.

◆ BLI_rng_get_int()

◆ BLI_rng_get_tri_sample_float_v2()

void BLI_rng_get_tri_sample_float_v2 ( struct RNG * rng,
const float v1[2],
const float v2[2],
const float v3[2],
float r_pt[2] )

Generate a random point inside given tri.

Definition at line 108 of file rand.cc.

References copy_v2_v2(), blender::RandomNumberGenerator::get_triangle_sample(), RNG::rng, and v2.

Referenced by BKE_mesh_remap_calc_faces_from_mesh().

◆ BLI_rng_get_tri_sample_float_v3()

void BLI_rng_get_tri_sample_float_v3 ( RNG * rng,
const float v1[3],
const float v2[3],
const float v3[3],
float r_pt[3] )

◆ BLI_rng_get_uint()

◆ BLI_rng_new()

◆ BLI_rng_new_srandom()

◆ BLI_rng_seed()

void BLI_rng_seed ( RNG * rng,
uint seed )

◆ BLI_rng_shuffle_array()

void BLI_rng_shuffle_array ( RNG * rng,
void * data,
uint elem_size_i,
uint elem_num )

Definition at line 120 of file rand.cc.

References BLI_rng_get_uint(), and free().

Referenced by BLI_array_randomize().

◆ BLI_rng_shuffle_bitmap()

void BLI_rng_shuffle_bitmap ( RNG * rng,
BLI_bitmap * bitmap,
uint bits_num )

Definition at line 144 of file rand.cc.

References BLI_BITMAP_SET, BLI_BITMAP_TEST, and BLI_rng_get_uint().

Referenced by BLI_bitmap_randomize().

◆ BLI_rng_skip()

void BLI_rng_skip ( struct RNG * rng,
int n )

Note that skipping is as slow as generating n numbers! Simulate getting n random values.

Note
Useful when threaded code needs consistent values, independent of task division.

Definition at line 162 of file rand.cc.

References RNG::rng, and blender::RandomNumberGenerator::skip().

Referenced by distribute_children_exec(), distribute_from_faces_exec(), distribute_from_verts_exec(), distribute_from_volume_exec(), exec_distribute_child(), and exec_distribute_parent().

◆ BLI_rng_srandom()

void BLI_rng_srandom ( struct RNG * rng,
unsigned int seed )

Use a hash table to create better seed.

Definition at line 68 of file rand.cc.

References RNG::rng, seed, and blender::RandomNumberGenerator::seed_random().

Referenced by BKE_brush_system_init(), BLI_array_frand(), BLI_hash_frand(), and BLI_rng_threaded_new().

◆ BLI_rng_thread_rand()

int BLI_rng_thread_rand ( RNG_THREAD_ARRAY * rngarr,
int thread )

Definition at line 253 of file rand.cc.

References BLI_rng_get_int(), and RNG_THREAD_ARRAY::rng_tab.

Referenced by texnoise().

◆ BLI_rng_threaded_free()

void BLI_rng_threaded_free ( RNG_THREAD_ARRAY * rngarr)

Definition at line 248 of file rand.cc.

References MEM_freeN().

Referenced by RE_texture_rng_exit().

◆ BLI_rng_threaded_new()

RNG_THREAD_ARRAY * BLI_rng_threaded_new ( void )

array versions for thread safe random generation

Definition at line 235 of file rand.cc.

References BLENDER_MAX_THREADS, BLI_rng_srandom(), MEM_mallocN, and RNG_THREAD_ARRAY::rng_tab.

Referenced by RE_texture_rng_init().

◆ BLI_thread_frand()

float BLI_thread_frand ( int thread)

Return a pseudo-random number N where 0.0f<=N<1.0f Allows up to BLENDER_MAX_THREADS threads to address

Definition at line 226 of file rand.cc.

References BLI_rng_get_float(), and rng_tab.

Referenced by ED_region_do_draw().

◆ BLI_thread_rand()

int BLI_thread_rand ( int thread)

Return a pseudo-random number N where 0<=N<(2^31) Allows up to BLENDER_MAX_THREADS threads to address

Definition at line 221 of file rand.cc.

References BLI_rng_get_int(), and rng_tab.

◆ BLI_thread_srandom()

void BLI_thread_srandom ( int thread,
unsigned int seed )

Better seed for the random number generator, using noise.cc hash[] Allows up to BLENDER_MAX_THREADS threads to address

Definition at line 208 of file rand.cc.

References BLENDER_MAX_THREADS, BLI_rng_get_uint(), BLI_rng_seed(), hash, rng_tab, and seed.

◆ halton_ex()

BLI_INLINE double halton_ex ( double invprimes,
double * offset )

Definition at line 262 of file rand.cc.

References e, and fabs().

Referenced by BLI_halton_1d(), BLI_halton_2d(), BLI_halton_2d_sequence(), and BLI_halton_3d().

◆ radical_inverse()

BLI_INLINE double radical_inverse ( uint n)

Definition at line 335 of file rand.cc.

Referenced by BLI_hammersley_1d(), and BLI_hammersley_2d_sequence().

Variable Documentation

◆ BLI_noise_hash_uchar_512

uchar BLI_noise_hash_uchar_512[512]

Definition at line 27 of file rand.cc.

◆ rng_tab

RNG rng_tab[BLENDER_MAX_THREADS]
static

Definition at line 206 of file rand.cc.

Referenced by BLI_thread_frand(), BLI_thread_rand(), and BLI_thread_srandom().