paul_random.h
IntroductionRandom number generation and noise functions for C/C++. DiscussionImplementation is included when PAUL_RANDOM_IMPLEMENTATION or PAUL_IMPLEMENTATION is defined.
Functions
cellular_automataGenerates a cellular automata pattern. void cellular_automata( rng_t *rng, unsigned int width, unsigned int height, unsigned int fill_chance, unsigned int smooth_iterations, unsigned int survive, unsigned int starve, uint8_t *dst); Parameters
fbmComputes fractal Brownian motion noise. float fbm( float x, float y, float z, float lacunarity, float gain, int octaves, noise_fn_t noise_fn); ParametersReturn ValueFBM noise value. fbm2DGenerates a 2D fractal Brownian motion noise map. void fbm2D( unsigned int width, unsigned int height, float z, float offset_x, float offset_y, float scale, float lacunarity, float gain, int octaves, noise_fn_t noise_fn, uint8_t *dst); Parameters
fbm3DGenerates a 3D fractal Brownian motion noise volume. void fbm3D( unsigned int width, unsigned int height, unsigned int depth, float offset_x, float offset_y, float offset_z, float scale, float lacunarity, float gain, int octaves, noise_fn_t noise_fn, uint8_t *dst); Parameters
noise_curl3DNumerically compute the curl of a vector field derived from noise samples. void noise_curl3D( float x, float y, float z, float eps, noise_fn_t noise_fn, float *out_x, float *out_y, float *out_z); Parametersnoise_tileable2DProduce tileable 2D noise by mapping coordinates onto a torus. float noise_tileable2D( float x, float y, float width, float height, noise_fn_t noise_fn); ParametersReturn ValueTileable noise value. normalize_to_unit_rangeNormalize an array of floats in-place to the [0,1] range. void normalize_to_unit_range( float *data, size_t n); Parametersperlin_noiseComputes Perlin noise at the given coordinates. float perlin_noise( float x, float y, float z); ParametersReturn ValuePerlin noise value in [-1, 1]. poisson_disc_free_listFrees a Poisson disc sampling point list. void poisson_disc_free_list( poisson_list_t *list); Parameterspoisson_disc_sample_foreachGenerates Poisson disc sampling points and calls a callback for each. void poisson_disc_sample_foreach( float width, float height, float min_dist, int max_attempts, poisson_check_callback_t check_fn, void *check_data, poisson_callback_t point_fn, void *point_data); Parameters
poisson_disc_sample_listGenerates Poisson disc sampling points and returns them in a list. poisson_list_t *poisson_disc_sample_list( float width, float height, float min_dist, int max_attempts, poisson_check_callback_t check_fn, void *user_data); Parameters
Return ValueAllocated list of points (must be freed with poisson_disc_free_list). ridged_multifractalRidged multifractal noise built from a base noise function. float ridged_multifractal( float x, float y, float z, float lacunarity, float gain, int octaves, noise_fn_t noise_fn); ParametersReturn ValueRidged multifractal value. rnd_exponentialSamples the exponential distribution with rate parameter lambda. float rnd_exponential( rng_t *rng, float lambda); ParametersReturn ValueA random float sampled from Exp(lambda). rnd_normalGenerates a normally-distributed random value using Box–Muller. float rnd_normal( rng_t *rng, float mean, float stddev); ParametersReturn ValueA random float approximately distributed N(mean, stddev^2). rnd_permutationFill a buffer with a random permutation of [0..n-1]. void rnd_permutation( rng_t *rng, uint32_t *out_perm, size_t n); Parametersrnd_randfGenerates a random float between 0.0 and 1.0. ParametersReturn ValueA random float value in [0.0, 1.0). rnd_randf_rangeGenerates a random float within a specified range. float rnd_randf_range( rng_t *rng, float min, float max); ParametersReturn ValueA random float value between min and max. rnd_randf_signedGenerates a uniformly-distributed float in [-1.0, 1.0). float rnd_randf_signed( rng_t *rng); ParametersReturn ValueA random float in [-1, 1). rnd_randiGenerates a random 64-bit unsigned integer. ParametersReturn ValueA random uint64_t value. rnd_randi_rangeGenerates a random integer within a specified range. int rnd_randi_range( rng_t *rng, int min, int max); ParametersReturn ValueA random int value between min and max. rnd_randi_range64Generates a uniform 64-bit integer in the inclusive range [min, max]. uint64_t rnd_randi_range64( rng_t *rng, uint64_t min, uint64_t max); ParametersReturn ValueA random uint64_t in [min, max]. If min >= max returns min. rnd_reseed_from_entropyReseed the RNG from OS-provided entropy. int rnd_reseed_from_entropy( rng_t *rng); ParametersReturn ValueNon-zero on success, zero on failure. rnd_seedInitializes the random number generator with a seed. Parametersrnd_shuffleIn-place Fisher–Yates shuffle for a contiguous array. void rnd_shuffle( rng_t *rng, void *array, size_t element_count, size_t element_size); Parametersrnd_shuffle_cbIn-place Fisher–Yates shuffle for an abstract container using a swap callback. void rnd_shuffle_cb( rng_t *rng, size_t element_count, rnd_swap_fn_t swap_fn, void *user_data); Parametersrnd_weighted_choiceChoose an index from an abstract list using a user-supplied weight callback. int rnd_weighted_choice( rng_t *rng, size_t n, rnd_weight_fn_t weight_fn, void *user_data); ParametersReturn ValueChosen index in [0,n-1] or -1 on error (e.g., zero total weight). rnd_weighted_choice_arrayChoose an index from a contiguous float weight array. int rnd_weighted_choice_array( rng_t *rng, const float *weights, size_t n); ParametersReturn ValueChosen index in [0,n-1] or -1 on error. simplex_noiseComputes Simplex noise at the given coordinates. float simplex_noise( float x, float y, float z); ParametersReturn ValueSimplex noise value. smootherstepfHigher-order smooth interpolation (smoothstep with zero derivatives at edges). float smootherstepf( float edge0, float edge1, float t); ParametersReturn ValueInterpolated value in [0,1]. smoothstepfSmooth Hermite interpolation between 0 and 1. float smoothstepf( float edge0, float edge1, float t); ParametersReturn ValueInterpolated value in [0,1]. turbulenceTurbulence-style noise (sum of absolute-valued octaves) built from a base noise function. float turbulence( float x, float y, float z, float lacunarity, float gain, int octaves, noise_fn_t noise_fn); ParametersReturn ValueTurbulence noise value. value_noiseComputes value noise at the given coordinates. float value_noise( float x, float y, float z); ParametersReturn ValueValue noise value in [-1, 1]. white_noiseComputes white noise at the given coordinates. float white_noise( float x, float y, float z); ParametersReturn ValueWhite noise value in [-1, 1]. worley_noiseComputes Worley (cellular) noise at the given coordinates. float worley_noise( float x, float y, float z); ParametersReturn ValueWorley noise value. Typedefs
noise_fn_tFunction pointer type for noise functions. typedef float( *noise_fn_t)( float, float, float); ParametersReturn ValueNoise value at the given coordinates. poisson_callback_tCallback function type for processing each generated point. typedef void ( *poisson_callback_t)( float x, float y, void *user_data); Parameterspoisson_check_callback_tCallback function type for checking if a point is valid. typedef int ( *poisson_check_callback_t)( float x, float y, void *user_data); ParametersReturn ValueNon-zero if the point is valid, zero otherwise. poisson_list_tStructure for storing a list of Poisson disc sampling points. typedef struct poisson_list { poisson_point_t *points; size_t count; size_t capacity; } poisson_list_t; Fieldspoisson_point_tStructure representing a 2D point for Poisson disc sampling. typedef struct poisson_point { float x; float y; } poisson_point_t; Fieldsrnd_swap_fn_tCallback type used to swap two elements in an abstract container. typedef void ( *rnd_swap_fn_t)( size_t a, size_t b, void *user_data); Parametersrnd_weight_fn_tCallback type used to provide a weight for a given index when choosing from a list. typedef double ( *rnd_weight_fn_t)( size_t idx, void *user_data); ParametersReturn ValueA non-negative double weight for the item. Negative values are treated as zero. rng_tRandom number generator state structure. Fields |