随机数的C API
通过 Cython 或像 CFFI 这样的 C 包装库可以访问以下各种分布.所有函数都接受一个 bitgen_t
作为它们的第一个参数.要从 Cython 或 C 访问这些函数,你必须链接到 npyrandom
静态库,它是 NumPy 发行版的一部分,位于 numpy/random/lib
中.请注意,你还必须链接到 npymath
,参见 在扩展中链接核心数学库.
-
type bitgen_t
bitgen_t
持有 BitGenerator 的当前状态和返回标准 C 类型的函数的指针,同时推进状态.
struct bitgen:
void *state
npy_uint64 (*next_uint64)(void *st) nogil
uint32_t (*next_uint32)(void *st) nogil
double (*next_double)(void *st) nogil
npy_uint64 (*next_raw)(void *st) nogil
ctypedef bitgen bitgen_t
有关使用这些函数的示例,请参见 扩展.
这些函数的命名遵循以下约定:
“standard” 指的是任何参数的参考值.例如,”standard_uniform” 表示在区间 0.0
到 1.0
上的均匀分布.
“fill” 函数将用 cnt
值填充提供的 out
.
名称中不含”standard”的函数需要额外的参数来描述分布.
名称中带有 inv
的函数基于较慢的逆方法,而不是显著更快的 ziggurat 查找算法.非 ziggurat 变体用于特殊情况和遗留兼容性.
-
double random_standard_uniform(bitgen_t *bitgen_state)
-
void random_standard_uniform_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out)
-
double random_standard_exponential(bitgen_t *bitgen_state)
-
void random_standard_exponential_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out)
-
void random_standard_exponential_inv_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out)
-
double random_standard_normal(bitgen_t *bitgen_state)
-
void random_standard_normal_fill(bitgen_t *bitgen_state, npy_intp count, double *out)
-
void random_standard_normal_fill_f(bitgen_t *bitgen_state, npy_intp count, float *out)
-
double random_standard_gamma(bitgen_t *bitgen_state, double shape)
-
float random_standard_uniform_f(bitgen_t *bitgen_state)
-
void random_standard_uniform_fill_f(bitgen_t *bitgen_state, npy_intp cnt, float *out)
-
float random_standard_exponential_f(bitgen_t *bitgen_state)
-
void random_standard_exponential_fill_f(bitgen_t *bitgen_state, npy_intp cnt, float *out)
-
void random_standard_exponential_inv_fill_f(bitgen_t *bitgen_state, npy_intp cnt, float *out)
-
float random_standard_normal_f(bitgen_t *bitgen_state)
-
float random_standard_gamma_f(bitgen_t *bitgen_state, float shape)
-
double random_normal(bitgen_t *bitgen_state, double loc, double scale)
-
double random_gamma(bitgen_t *bitgen_state, double shape, double scale)
-
float random_gamma_f(bitgen_t *bitgen_state, float shape, float scale)
-
double random_exponential(bitgen_t *bitgen_state, double scale)
-
double random_uniform(bitgen_t *bitgen_state, double lower, double range)
-
double random_beta(bitgen_t *bitgen_state, double a, double b)
-
double random_chisquare(bitgen_t *bitgen_state, double df)
-
double random_f(bitgen_t *bitgen_state, double dfnum, double dfden)
-
double random_standard_cauchy(bitgen_t *bitgen_state)
-
double random_pareto(bitgen_t *bitgen_state, double a)
-
double random_weibull(bitgen_t *bitgen_state, double a)
-
double random_power(bitgen_t *bitgen_state, double a)
-
double random_laplace(bitgen_t *bitgen_state, double loc, double scale)
-
double random_gumbel(bitgen_t *bitgen_state, double loc, double scale)
-
double random_logistic(bitgen_t *bitgen_state, double loc, double scale)
-
double random_lognormal(bitgen_t *bitgen_state, double mean, double sigma)
-
double random_rayleigh(bitgen_t *bitgen_state, double mode)
-
double random_standard_t(bitgen_t *bitgen_state, double df)
-
double random_noncentral_chisquare(bitgen_t *bitgen_state, double df, double nonc)
-
double random_noncentral_f(bitgen_t *bitgen_state, double dfnum, double dfden, double nonc)
-
double random_wald(bitgen_t *bitgen_state, double mean, double scale)
-
double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa)
-
double random_triangular(bitgen_t *bitgen_state, double left, double mode, double right)
-
npy_int64 random_poisson(bitgen_t *bitgen_state, double lam)
-
npy_int64 random_negative_binomial(bitgen_t *bitgen_state, double n, double p)
-
type binomial_t
typedef struct s_binomial_t {
int has_binomial; /* !=0: following parameters initialized for binomial */
double psave;
RAND_INT_TYPE nsave;
double r;
double q;
double fm;
RAND_INT_TYPE m;
double p1;
double xm;
double xl;
double xr;
double c;
double laml;
double lamr;
double p2;
double p3;
double p4;
} binomial_t;
-
npy_int64 random_binomial(bitgen_t *bitgen_state, double p, npy_int64 n, binomial_t *binomial)
-
npy_int64 random_logseries(bitgen_t *bitgen_state, double p)
-
npy_int64 random_geometric_search(bitgen_t *bitgen_state, double p)
-
npy_int64 random_geometric_inversion(bitgen_t *bitgen_state, double p)
-
npy_int64 random_geometric(bitgen_t *bitgen_state, double p)
-
npy_int64 random_zipf(bitgen_t *bitgen_state, double a)
-
npy_int64 random_hypergeometric(bitgen_t *bitgen_state, npy_int64 good, npy_int64 bad, npy_int64 sample)
-
npy_uint64 random_interval(bitgen_t *bitgen_state, npy_uint64 max)
-
void random_multinomial(bitgen_t *bitgen_state, npy_int64 n, npy_int64 *mnix, double *pix, npy_intp d, binomial_t *binomial)
-
int random_multivariate_hypergeometric_count(bitgen_t *bitgen_state, npy_int64 total, size_t num_colors, npy_int64 *colors, npy_int64 nsample, size_t num_variates, npy_int64 *variates)
-
void random_multivariate_hypergeometric_marginals(bitgen_t *bitgen_state, npy_int64 total, size_t num_colors, npy_int64 *colors, npy_int64 nsample, size_t num_variates, npy_int64 *variates)
生成一个整数
-
npy_int64 random_positive_int64(bitgen_t *bitgen_state)
-
npy_int32 random_positive_int32(bitgen_t *bitgen_state)
-
npy_int64 random_positive_int(bitgen_t *bitgen_state)
-
npy_uint64 random_uint(bitgen_t *bitgen_state)
在闭区间 [off, off + rng] 中生成随机的 uint64 数字.
-
npy_uint64 random_bounded_uint64(bitgen_t *bitgen_state, npy_uint64 off, npy_uint64 rng, npy_uint64 mask, bool use_masked)