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.
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.
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.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
20 #ifndef __BLI_RAND_H__
21 #define __BLI_RAND_H__
25 * \brief Random number functions.
28 /* RNG is an abstract random number generator type that avoids using globals.
29 * Always use this instead of the global RNG unless you have a good reason,
30 * the global RNG is not thread safe and will not give repeatable results.
33 typedef struct RNG RNG;
35 struct RNG_THREAD_ARRAY;
36 typedef struct RNG_THREAD_ARRAY RNG_THREAD_ARRAY;
38 struct RNG *BLI_rng_new(unsigned int seed);
39 struct RNG *BLI_rng_new_srandom(unsigned int seed);
40 struct RNG *BLI_rng_copy(struct RNG *rng) ATTR_NONNULL(1);
41 void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1);
43 void BLI_rng_seed(struct RNG *rng, unsigned int seed) ATTR_NONNULL(1);
44 void BLI_rng_srandom(struct RNG *rng, unsigned int seed) ATTR_NONNULL(1);
45 void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len) ATTR_NONNULL(1, 2);
46 int BLI_rng_get_int(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
47 unsigned int BLI_rng_get_uint(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
48 double BLI_rng_get_double(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
49 float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
50 void BLI_rng_get_float_unit_v2(struct RNG *rng, float v[2]) ATTR_NONNULL(1, 2);
51 void BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]) ATTR_NONNULL(1, 2);
52 void BLI_rng_get_tri_sample_float_v2(
53 struct RNG *rng, const float v1[2], const float v2[2], const float v3[2],
54 float r_pt[2]) ATTR_NONNULL();
55 void BLI_rng_shuffle_array(struct RNG *rng, void *data, unsigned int elem_size_i, unsigned int elem_tot) ATTR_NONNULL(1, 2);
57 /** Note that skipping is as slow as generating n numbers! */
58 void BLI_rng_skip(struct RNG *rng, int n) ATTR_NONNULL(1);
60 /* fill an array with random numbers */
61 void BLI_array_frand(float *ar, int count, unsigned int seed);
63 /** Return a pseudo-random (hash) float from an integer value */
64 float BLI_hash_frand(unsigned int seed) ATTR_WARN_UNUSED_RESULT;
66 /** Shuffle an array randomly using the given seed.
67 * contents. This routine does not use nor modify
68 * the state of the BLI random number generator.
70 void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_tot, unsigned int seed);
73 /** Better seed for the random number generator, using noise.c hash[] */
74 /** Allows up to BLENDER_MAX_THREADS threads to address */
75 void BLI_thread_srandom(int thread, unsigned int seed);
77 /** Return a pseudo-random number N where 0<=N<(2^31) */
78 /** Allows up to BLENDER_MAX_THREADS threads to address */
79 int BLI_thread_rand(int thread) ATTR_WARN_UNUSED_RESULT;
81 /** Return a pseudo-random number N where 0.0f<=N<1.0f */
82 /** Allows up to BLENDER_MAX_THREADS threads to address */
83 float BLI_thread_frand(int thread) ATTR_WARN_UNUSED_RESULT;
85 /** array versions for thread safe random generation */
86 RNG_THREAD_ARRAY *BLI_rng_threaded_new(void);
87 void BLI_rng_threaded_free(struct RNG_THREAD_ARRAY *rngarr) ATTR_NONNULL(1);
88 int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread) ATTR_WARN_UNUSED_RESULT;
90 /** Low-discrepancy sequences **/
92 /** Return the _n_th number of the given low-discrepancy sequence. */
93 void BLI_halton_1D(unsigned int prime, double offset, int n, double *r);
94 void BLI_halton_2D(unsigned int prime[2], double offset[2], int n, double *r);
95 void BLI_halton_3D(unsigned int prime[3], double offset[3], int n, double *r);
96 void BLI_hammersley_1D(unsigned int n, double *r);
98 /** Return the whole low-discrepancy sequence up to _n_. */
99 void BLI_halton_2D_sequence(unsigned int prime[2], double offset[2], int n, double *r);
100 void BLI_hammersley_2D_sequence(unsigned int n, double *r);
102 #endif /* __BLI_RAND_H__ */