GwyRandGenSet — Set of random number generators
GwyRandGenSet |
#include <libgwyddion/gwyddion.h>
GwyRandGenSet represents a set of pseudoradnom number generators initialised together, but each producing a different sequence of numbers. This is useful when you use pseudorandom number generators to optionally randomize several different things. Using a common generator would require always generating exactly the same number of random numbers, even for quantities you do not want randomized, in order to keep the random number sequences stable.
GwyRandGenSet also provides functions sample a few common distributions such as Gaussian or exponential. You can
also get the underlying GRand generators with gwy_rand_gen_set_rng()
and use them directly. It is useful for
generating integers as GwyRandGenSet does not wrap the GRand integer functions.
It should be noted that the individual sampling functions may advance the generator state differently. This means
1 2 |
x = gwy_rand_gen_set_gaussian(rngset, 0, 1.0); y = gwy_rand_gen_set_gaussian(rngset, 0, 1.0); |
may produce a different value of y
than
1 2 |
x = gwy_rand_gen_set_exponential(rngset, 0, 1.0); y = gwy_rand_gen_set_gaussian(rngset, 0, 1.0); |
even if the initial generator state was the same.
GwyRandGenSet *
gwy_rand_gen_set_new (guint n
);
Creates a new set of pseudorandom number generators.
The generators are initialised to random states.
n |
The number of generators. |
A new set of pseudorandom number generators.
Since: 2.37
void gwy_rand_gen_set_init (GwyRandGenSet *rngset
,guint seed
);
Initialises a set of pseudorandom number generators using an integer seed.
rngset |
A set of pseudorandom number generators. |
|
seed |
The seed used to initialise the generators. |
Since: 2.37
void
gwy_rand_gen_set_free (GwyRandGenSet *rngset
);
Destroys a set of pseudorandom number generators.
If you obtained individual generators using gwy_rand_gen_set_rng()
you may not use them any more after calling this
function.
rngset |
A set of pseudorandom number generators. |
Since: 2.37
GRand * gwy_rand_gen_set_rng (GwyRandGenSet *rngset
,guint i
);
Obtains a single generator from a set of pseudorandom number generators.
The generator can be used to produce random numbers in any way for which you find the provided methods insufficient. However, if you reseed it manually, number sequence stability will be broken because sampling functions may keep persistent information between calls.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
A pseudorandom number generator from rngset
.
Since: 2.37
gdouble gwy_rand_gen_set_range (GwyRandGenSet *rngset
,guint i
,gdouble lower
,gdouble upper
);
Samples from a uniform distribution over given interval using one generator from a pseudorandom number generator set.
The generated number always lies inside the interval, neither endpoint value is ever returned. Note if there are
no representable real numbers between lower
and upper
this function will never terminate. You must ensure upper
is sufficiently larger than lower
.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
lower |
Lower limit of the range. |
|
upper |
Upper limit of the range. |
A pseudorandom number.
Since: 2.37
gdouble gwy_rand_gen_set_uniform (GwyRandGenSet *rngset
,guint i
,gdouble sigma
);
Samples from a centered uniform distribution using one generator from a pseudorandom number generator set.
The mean value of the distribution is zero, the rms value is given by sigma
.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
sigma |
Rms of the distribution. |
A pseudorandom number.
Since: 2.37
gdouble gwy_rand_gen_set_gaussian (GwyRandGenSet *rngset
,guint i
,gdouble sigma
);
Samples from a centered Gaussian distribution using one generator from a pseudorandom number generator set.
The mean value of the distribution is zero, the rms value is given by sigma
.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
sigma |
Rms of the distribution. |
A pseudorandom number.
Since: 2.37
gdouble gwy_rand_gen_set_exponential (GwyRandGenSet *rngset
,guint i
,gdouble sigma
);
Samples from a centered exponential distribution using one generator from a pseudorandom number generator set.
The mean value of the distribution is zero, the rms value is given by sigma
.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
sigma |
Rms of the distribution. |
A pseudorandom number.
Since: 2.37
gdouble gwy_rand_gen_set_triangular (GwyRandGenSet *rngset
,guint i
,gdouble sigma
);
Samples from a centered triangular distribution using one generator from a pseudorandom number generator set.
The mean value of the distribution is zero, the rms value is given by sigma
.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
sigma |
Rms of the distribution. |
A pseudorandom number.
Since: 2.37
gdouble gwy_rand_gen_set_multiplier (GwyRandGenSet *rngset
,guint i
,gdouble range
);
Samples from a multiplier distribution using one generator from a pseudorandom number generator set.
The multiplier distribution is triangular distribution centered at 1, with values from [1-range
, 1+range
].
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
range |
Half-range of the distribution. |
A pseudorandom number.
Since: 2.37
gdouble gwy_rand_gen_set_double (GwyRandGenSet *rngset
,guint i
);
Samples uniform distribution over [0,1) using one generator from a pseudorandom number generator set.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
A pseudorandom number.
Since: 2.37
guint32 gwy_rand_gen_set_int (GwyRandGenSet *rngset
,guint i
);
Samples a 32bit integer using a generator from a pseudorandom number generator set.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
A pseudorandom number.
Since: 2.37
guint * gwy_rand_gen_set_choose_shuffle (GwyRandGenSet *rngset
,guint i
,guint n
,guint nchoose
);
Chooses randomly a subset of indices, in random order.
The function creates an array containing integers from the set {0, 1, 2, ..., n-1}, each at most once, in random order.
To generate a permutation, simply pass nchoose
equal to n
.
rngset |
A set of pseudorandom number generators. |
|
i |
Index of a generator from the set. |
|
n |
Total number of possible indices. |
|
nchoose |
Number of values to choose (at most equal to |
A newly allocated array of nchoose
integers.
Since: 2.46
void gwy_rand_gen_set_fill_doubles (GwyRandGenSet *rngset
,gdouble *random_numbers
,gint n
);
Fill an array with random doubles from a pseudorandom number generator set
If rngset
has N
generators then the array is split into N
equally sized blocks (at least approximately) and
each block is filled from a different generator. The filling of individual blocks is run multi-threaded (if
parallelisation is enabled). The result does not depend on the number of threads, allowing parallelisation of
random number generation.
Note that the number of generators N
must be a fixed constant for reproducible results. It must not depend on the
actual number of processor cores or a similar variable quantity.
rngset |
A set of pseudorandom number generators. |
|
random_numbers |
Array to be filled with random numbers. |
|
n |
How many numbers to generated. |
Since: 2.59
void gwy_rand_gen_set_fill_ints (GwyRandGenSet *rngset
,guint32 *random_numbers
,gint n
);
Fill an array with random integers from a pseudorandom number generator set
See gwy_rand_gen_set_fill_doubles()
for discussion and caveats.
rngset |
A set of pseudorandom number generators. |
|
random_numbers |
Array to be filled with random numbers. |
|
n |
How many numbers to generated. |
Since: 2.59
typedef struct _GwyRandGenSet GwyRandGenSet;
GwyRandGenSet is an opaque data structure and should be only manipulated with the functions below.
Since: 2.37