Movatterモバイル変換


[0]ホーム

URL:


Random.user {base}R Documentation

User-supplied Random Number Generation

Description

FunctionRNGkind allows user-coded uniform andnormal random number generators to be supplied. The details are givenhere.

Details

A user-specified uniform RNG is called from entry points indynamically-loaded compiled code. The user must supply the entry pointuser_unif_rand, which takes no arguments and returns apointer to a double. The example below will show the generalpattern. The generator should have at least 25 bits of precision.

Optionally, the user can supply the entry pointuser_unif_init,which is called with anunsigned int argument whenRNGkind (orset.seed) is called, and is intendedto be used to initialize the user's RNG code. The argument is intendedto be used to set the ‘seeds’; it is theseed argument toset.seed or an essentially random seed ifRNGkindis called.

If only these functions are supplied, no information about thegenerator's state is recorded in.Random.seed. Optionally,functionsuser_unif_nseed anduser_unif_seedloc can besupplied which are called with no arguments and should return pointersto the number of seeds and to an integer (specifically, ‘⁠Int32⁠’)array of seeds. Calls toGetRNGstate andPutRNGstatewill then copy this array to and from.Random.seed.

A user-specified normal RNG is specified by a single entry pointuser_norm_rand, which takes no arguments and returns apointer to a double.

Warning

As with all compiled code, mis-specifying thesefunctions can crashR. Do include the ‘R_ext/Random.h’header file for type checking.

Examples

## Not run: ##  Marsaglia's congruential PRNG#include <R_ext/Random.h>static Int32 seed;static double res;static int nseed = 1;double * user_unif_rand(void){    seed = 69069 * seed + 1;    res = seed * 2.32830643653869e-10;    return &res;}void  user_unif_init(Int32 seed_in) { seed = seed_in; }int * user_unif_nseed(void) { return &nseed; }int * user_unif_seedloc(void) { return (int *) &seed; }/*  ratio-of-uniforms for normal  */#include <math.h>static double x;double * user_norm_rand(void){    double u, v, z;    do {        u = unif_rand();        v = 0.857764 * (2. * unif_rand() - 1);        x = v/u; z = 0.25 * x * x;        if (z < 1. - u) break;        if (z > 0.259/u + 0.35) continue;    } while (z > -log(u));    return &x;}## Use under Unix:R CMD SHLIB urand.cR> dyn.load("urand.so")> RNGkind("user")> runif(10)> .Random.seed> RNGkind(, "user")> rnorm(10)> RNGkind()[1] "user-supplied" "user-supplied"## End(Not run)

[Packagebase version 4.6.0Index]

[8]ページ先頭

©2009-2025 Movatter.jp