NcmRNG

NcmRNG — Encapsulated GNU Scientific Library (GSL) random number generator with support for multhreading.

Stability Level

Stable, unless otherwise indicated

Functions

Properties

char * algorithm Read / Write / Construct
gulong seed Read / Write
char * state Read / Write

Types and Values

#define NCM_TYPE_RNG
struct NcmRNGClass
  NcmRNG

Object Hierarchy

    GBoxed
    ╰── NcmRNGDiscrete
    GObject
    ╰── NcmRNG

Includes

#include <numcosmo/math/ncm_rng.h>

Description

This object encapsulates the GNU Scientific Library (GSL) pseudo random number generator (PRNG). Its main purpose is to add support for saving and loading state and multhreading. For more information about the GSL routines see both links: random number generation and random number distributions.

Functions

ncm_rng_discrete_new ()

NcmRNGDiscrete *
ncm_rng_discrete_new (const gdouble *weights,
                      const guint n);

Creates a new NcmRNGDiscrete. This object is used to generate random numbers from a discrete distribution for the given set of weights.

Parameters

weights

array of weights.

[array length=n]

n

number of elements in weights

 

Returns

a new NcmRNGDiscrete.

[transfer full]


ncm_rng_discrete_copy ()

NcmRNGDiscrete *
ncm_rng_discrete_copy (NcmRNGDiscrete *rng);

Creates a copy of rng_discrete .

Parameters

rng

a NcmRNGDiscrete

 

Returns

a copy of rng_discrete .

[transfer full]


ncm_rng_discrete_free ()

void
ncm_rng_discrete_free (NcmRNGDiscrete *rng);

Frees the memory allocated by rng .

Parameters

rng

a NcmRNGDiscrete

 

ncm_rng_new ()

NcmRNG *
ncm_rng_new (const gchar *algo);

Creates a new NcmRNG using the algorithm algo . See the list of algorithms here. If algo is NULL the default algorithm and seed are used. See this link for more details.

Parameters

algo

algorithm name.

[allow-none]

Returns

a new NcmRNG.

[transfer full]


ncm_rng_seeded_new ()

NcmRNG *
ncm_rng_seeded_new (const gchar *algo,
                    gulong seed);

Creates a new NcmRNG using the algorithm algo . See the list of algorithms here. If algo is NULL the default algorithm is used. See this link for more details.

Parameters

algo

algorithm name.

[allow-none]

seed

seed used to initialize the PRNG

 

Returns

a new NcmRNG.

[transfer full]


ncm_rng_ref ()

NcmRNG *
ncm_rng_ref (NcmRNG *rng);

Increases the reference count of rng by one.

Parameters

rng

a NcmRNG

 

Returns

rng .

[transfer full]


ncm_rng_free ()

void
ncm_rng_free (NcmRNG *rng);

Decreases the reference count of rng by one.

Parameters

rng

a NcmRNG

 

ncm_rng_clear ()

void
ncm_rng_clear (NcmRNG **rng);

Decreases the reference count of *rng by one and sets *rng to NULL.

Parameters

rng

a NcmRNG

 

ncm_rng_lock ()

void
ncm_rng_lock (NcmRNG *rng);

Locks rng .

Parameters

rng

a NcmRNG

 

ncm_rng_unlock ()

void
ncm_rng_unlock (NcmRNG *rng);

Unlocks rng .

Parameters

rng

a NcmRNG

 

ncm_rng_get_algo ()

const gchar *
ncm_rng_get_algo (NcmRNG *rng);

Gets the name of the algorithm.

Parameters

rng

a NcmRNG

 

Returns

algorithm name.

[transfer none]


ncm_rng_get_state ()

gchar *
ncm_rng_get_state (NcmRNG *rng);

Gets the state of the algorithm in Base64. It can be a very large string depending on the underlining state.

Parameters

rng

a NcmRNG

 

Returns

algorithm state.

[transfer full]


ncm_rng_set_algo ()

void
ncm_rng_set_algo (NcmRNG *rng,
                  const gchar *algo);

Sets the PRNG algorithm.

Parameters

rng

a NcmRNG

 

algo

algorithm name

 

ncm_rng_set_state ()

void
ncm_rng_set_state (NcmRNG *rng,
                   const gchar *state);

Sets the PRNG algorithm state.

Parameters

rng

a NcmRNG

 

state

algorithm state

 

ncm_rng_check_seed ()

gboolean
ncm_rng_check_seed (NcmRNG *rng,
                    gulong seed);

Check if the seed was already used by any NcmRNG.

Parameters

rng

a NcmRNG

 

seed

seed for the PRNG

 

Returns

TRUE if seed was never used and FALSE otherwise.


ncm_rng_set_seed ()

void
ncm_rng_set_seed (NcmRNG *rng,
                  gulong seed);

Sets the PRNG algorithm seed.

Parameters

rng

a NcmRNG

 

seed

seed for the PRNG

 

ncm_rng_get_seed ()

gulong
ncm_rng_get_seed (NcmRNG *rng);

This functions returns the seed used to initialize the PRNG.

Parameters

rng

a NcmRNG

 

Returns

rng 's seed .


ncm_rng_set_random_seed ()

void
ncm_rng_set_random_seed (NcmRNG *rng,
                         gboolean allow_colisions);

Sets the algorithm seed using a PRNG seeded by /dev/urandom (Unix/Linux) or current time, when the first is not available (see g_rand_new()). If allow_colisions is FALSE this function will set the first unused seed generated.

Parameters

rng

a NcmRNG

 

allow_colisions

a gboolean

 

ncm_rng_pool_get ()

NcmRNG *
ncm_rng_pool_get (const gchar *name);

Gets the NcmRNG named name from the pool. If it doesn't exists, it creates one, add it to the pool and returns it.

Parameters

name

a string

 

Returns

the NcmRNG named name .

[transfer full]


ncm_rng_gen_ulong ()

gulong
ncm_rng_gen_ulong (NcmRNG *rng);

This function returns a random unsigned integer from the uniform distribution.

Parameters

rng

a NcmRNG

 

Returns

a random unsigned long from the uniform distribution.


ncm_rng_uniform_int_gen ()

gulong
ncm_rng_uniform_int_gen (NcmRNG *rng,
                         gulong n);

This function returns a random number drawn from the uniform distribution between zero and n .

Parameters

rng

a NcmRNG

 

n

upper limit

 

Returns

a random number from the uniform distribution.


ncm_rng_uniform01_gen ()

gdouble
ncm_rng_uniform01_gen (NcmRNG *rng);

This function returns a random number drawn from the uniform distribution between zero and one $[0,1)$.

Parameters

rng

a NcmRNG

 

Returns

a random number from the uniform distribution.


ncm_rng_uniform01_pos_gen ()

gdouble
ncm_rng_uniform01_pos_gen (NcmRNG *rng);

This function returns a random number drawn from the uniform distribution between zero and one $(0,1)$.

Parameters

rng

a NcmRNG

 

Returns

a random number from the uniform distribution.


ncm_rng_uniform_gen ()

gdouble
ncm_rng_uniform_gen (NcmRNG *rng,
                     const gdouble xl,
                     const gdouble xu);

This functions returns a random number drawn from the uniform distribution between the values xl and xu .

Parameters

rng

a NcmRNG

 

xl

lower value

 

xu

upper value

 

Returns

a random number from the uniform distribution.


ncm_rng_gaussian_gen ()

gdouble
ncm_rng_gaussian_gen (NcmRNG *rng,
                      const gdouble mu,
                      const gdouble sigma);

This function returns a random number drawn from the Gaussian distribution, with mean mu and standard deviation sigma .

Parameters

rng

a NcmRNG

 

mu

mean

 

sigma

standard deviation

 

Returns

a random number from the Gaussian distribution.


ncm_rng_ugaussian_gen ()

gdouble
ncm_rng_ugaussian_gen (NcmRNG *rng);

This function returns a random number drwan from the Gaussian distribution, with mean zero and standard deviation one. Equivalent as above but with mean = 0 and sigma = 1.

Parameters

rng

a NcmRNG

 

Returns

a random number from the Gaussian distribution.


ncm_rng_gaussian_tail_gen ()

gdouble
ncm_rng_gaussian_tail_gen (NcmRNG *rng,
                           const gdouble a,
                           const gdouble sigma);

This function returns a random number drawn from the upper tail of the Gaussian distribution with standard deviation sigma . The value returned is larger than the lower limit a , which must be positive.

Parameters

rng

a NcmRNG

 

a

positive lower limit

 

sigma

standard deviation

 

Returns

a random number from the Gaussian distribution tail.


ncm_rng_exponential_gen ()

gdouble
ncm_rng_exponential_gen (NcmRNG *rng,
                         const gdouble mu);

This function returns a random number drawn from the exponential distribution with scale parameter (mean) mu .

Parameters

rng

a NcmRNG

 

mu

scale parameter

 

Returns

a random number from the exponential distribution.


ncm_rng_laplace_gen ()

gdouble
ncm_rng_laplace_gen (NcmRNG *rng,
                     const gdouble a);

This function returns a random number drawn from the Laplace distribution with width a .

Parameters

rng

a NcmRNG

 

a

width of the distribution

 

Returns

a random number from the Laplace distribution.


ncm_rng_exppow_gen ()

gdouble
ncm_rng_exppow_gen (NcmRNG *rng,
                    const gdouble a,
                    const gdouble b);

This function returns a random number drawn from the exponential power distribution with scale parameter a and exponent b .

Parameters

rng

a NcmRNG

 

a

scale parameter

 

b

exponent

 

Returns

a random number from the exponential power distribution.


ncm_rng_beta_gen ()

gdouble
ncm_rng_beta_gen (NcmRNG *rng,
                  const gdouble a,
                  const gdouble b);

This function returns a random number drawn from the beta distribution with shape parameters a and b . The shape parameters must be positive.

Parameters

rng

a NcmRNG

 

a

shape parameter

 

b

shape parameter

 

Returns

a random number from the beta distribution.


ncm_rng_gamma_gen ()

gdouble
ncm_rng_gamma_gen (NcmRNG *rng,
                   const gdouble a,
                   const gdouble b);

This function returns a random number drawn from the gamma distribution with shape parameter a and scale parameter b .

Parameters

rng

a NcmRNG

 

a

shape parameter

 

b

scale parameter

 

Returns

a random number from the gamma distribution.


ncm_rng_chisq_gen ()

gdouble
ncm_rng_chisq_gen (NcmRNG *rng,
                   const gdouble nu);

This function returns a random number drawn from the Chi-square Distribution, with $\nu$ degrees of freedom.

Parameters

rng

a NcmRNG

 

nu

degrees of freedom $\nu$

 

Returns

a random number from Chi-square distribution.


ncm_rng_poisson_gen ()

gdouble
ncm_rng_poisson_gen (NcmRNG *rng,
                     const gdouble mu);

This function returns a random number drawn from the Poisson distribution, with frequency mu .

Parameters

rng

a NcmRNG

 

mu

degrees of freedom $\nu$

 

Returns

a random number from the Poisson distribution.


ncm_rng_rayleigh_gen ()

gdouble
ncm_rng_rayleigh_gen (NcmRNG *rng,
                      const gdouble sigma);

This function returns a random number drawn from the Rayleigh distribution with scale parameter sigma .

Parameters

rng

a NcmRNG

 

sigma

scale parameter

 

Returns

a random number from the Rayleigh distribution.


ncm_rng_discrete_gen ()

gsize
ncm_rng_discrete_gen (NcmRNG *rng,
                      NcmRNGDiscrete *rng_discrete);

This function returns a random number drawn from the discrete distribution. The weights must created using ncm_rng_discrete_new().

Parameters

rng

a NcmRNG

 

rng_discrete

a NcmRNGDiscrete

 

ncm_rng_sample ()

void
ncm_rng_sample (NcmRNG *rng,
                void *dest,
                size_t k,
                void *src,
                size_t n,
                size_t size);

This function fills the array dest with k elements from the array src . The elements are chosen randomly using the algorithm (sample with replecement) in gsl_ran_sample().

Parameters

rng

a NcmRNG

 

dest

an array of k elements of size size

 

k

number of elements in dest

 

src

an array of n elements of size size

 

n

number of elements in src

 

size

size of each element in dest and src

 

ncm_rng_choose ()

void
ncm_rng_choose (NcmRNG *rng,
                void *dest,
                size_t k,
                void *src,
                size_t n,
                size_t size);

This function fills the array dest with k elements from the array src . The elements are chosen randomly using the algorithm (choose with replecement) in gsl_ran_choose().

Parameters

rng

a NcmRNG

 

dest

an array of k elements of size size

 

k

number of elements in dest

 

src

an array of n elements of size size

 

n

number of elements in src

 

size

size of each element in dest and src

 

ncm_rng_multinomial ()

void
ncm_rng_multinomial (NcmRNG *rng,
                     gsize K,
                     guint N,
                     const gdouble *p,
                     guint *n);

This function fills the array n with K elements using a multinomial distribution defined by the array p .

Parameters

rng

a NcmRNG

 

K

number of possible outcomes

 

N

number of trials

 

p

array of probabilities.

[array length=K][element-type gdouble]

n

array of counts.

[array length=K][element-type guint]

ncm_rng_bivariate_gaussian_gen ()

void
ncm_rng_bivariate_gaussian_gen (NcmRNG *rng,
                                const gdouble sigma_x,
                                const gdouble sigma_y,
                                const gdouble rho,
                                gdouble *x,
                                gdouble *y);

This function returns a random number drawn from the Bivariate Gaussian distribution, with standard deviations sigma_x and sigma_y and correlation coefficient rho . The correlation coefficient must be in the range $-1 \leq \rho \leq 1$.

Parameters

rng

a NcmRNG

 

sigma_x

standard deviation

 

sigma_y

standard deviation

 

rho

correlation coefficient

 

x

random number from the Bivariate Gaussian distribution.

[out]

y

random number from the Bivariate Gaussian distribution.

[out]

Types and Values

NCM_TYPE_RNG

#define NCM_TYPE_RNG (ncm_rng_get_type ())

struct NcmRNGClass

struct NcmRNGClass {
};

NcmRNG

typedef struct _NcmRNG NcmRNG;

Property Details

The “algorithm” property

  “algorithm”                char *

The name of the pseudo random number algorithm to be used from GNU Scientific Library (GSL). A list of the available algorithms can be find here.

Owner: NcmRNG

Flags: Read / Write / Construct

Default value: "mt19937"


The “seed” property

  “seed”                     gulong

Pseudo random number algorithm seed.

Owner: NcmRNG

Flags: Read / Write


The “state” property

  “state”                    char *

Pseudo random number algorithm state.

Owner: NcmRNG

Flags: Read / Write

Default value: NULL