xrandom¶
Defined in xtensor/xrandom.hpp
Warning
xtensor uses a lazy generator for random numbers. You need to assign them or use eval to keep the generated values consistent.
-
default_engine_type &
xt::random::get_default_random_engine()¶ Returns a reference to the default random number engine.
-
void
xt::random::seed(seed_type seed)¶ Seeds the default random number generator with
seed.- Parameters
seed: The seed
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::rand(const S &shape, T lower, T upper, E &engine)¶ xexpression with specified
shapecontaining uniformly distributed random numbers in the interval fromlowertoupper, excluding upper.Numbers are drawn from
std::uniform_real_distribution.- Parameters
shape: shape of resulting xexpressionlower: lower boundupper: upper boundengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::randint(const S &shape, T lower, T upper, E &engine)¶ xexpression with specified
shapecontaining uniformly distributed random integers in the interval fromlowertoupper, excluding upper.Numbers are drawn from
std::uniform_int_distribution.- Parameters
shape: shape of resulting xexpressionlower: lower boundupper: upper boundengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::randn(const S &shape, T mean, T std_dev, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from the Normal (Gaussian) random number distribution with meanmeanand standard deviationstd_dev.Numbers are drawn from
std::normal_distribution.- Parameters
shape: shape of resulting xexpressionmean: mean of normal distributionstd_dev: standard deviation of normal distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classD= double, classE= random::default_engine_type>
autoxt::random::binomial(const S &shape, T trials, D prob, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from the binomial random number distribution fortrialstrials with probability of success equal toprob.Numbers are drawn from
std::binomial_distribution.- Parameters
shape: shape of resulting xexpressiontrials: number of Bernoulli trialsprob: probability of success of each trialengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classD= double, classE= random::default_engine_type>
autoxt::random::geometric(const S &shape, D prob, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a gemoetric random number distribution with probability of success equal toprobfor each of the Bernoulli trials.Numbers are drawn from
std::geometric_distribution.- Parameters
shape: shape of resulting xexpressionprob: probability of success of each trialengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classD= double, classE= random::default_engine_type>
autoxt::random::negative_binomial(const S &shape, T k, D prob, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a negative binomial random number distribution (also known as Pascal distribution) that returns the number of successes beforektrials with probability of success equal toprobfor each of the Bernoulli trials.Numbers are drawn from
std::negative_binomial_distribution.- Parameters
shape: shape of resulting xexpressionk: number of unsuccessful trialsprob: probability of success of each trialengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classD= double, classE= random::default_engine_type>
autoxt::random::poisson(const S &shape, D rate, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a Poisson random number distribution with raterateNumbers are drawn from
std::poisson_distribution.- Parameters
shape: shape of resulting xexpressionrate: rate of Poisson distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::exponential(const S &shape, T rate, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a exponential random number distribution with raterateNumbers are drawn from
std::exponential_distribution.- Parameters
shape: shape of resulting xexpressionrate: rate of exponential distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::gamma(const S &shape, T alpha, T beta, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a gamma random number distribution with shapealphaand scalebetaNumbers are drawn from
std::gamma_distribution.- Parameters
shape: shape of resulting xexpressionalpha: shape of the gamma distributionbeta: scale of the gamma distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::weibull(const S &shape, T a, T b, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a Weibull random number distribution with shapeaand scalebNumbers are drawn from
std::weibull_distribution.- Parameters
shape: shape of resulting xexpressiona: shape of the weibull distributionb: scale of the weibull distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::extreme_value(const S &shape, T a, T b, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a extreme value random number distribution with shapeaand scalebNumbers are drawn from
std::extreme_value_distribution.- Parameters
shape: shape of resulting xexpressiona: shape of the extreme value distributionb: scale of the extreme value distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::lognormal(const S &shape, T mean, T std_dev, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from the Log-Normal random number distribution with meanmeanand standard deviationstd_dev.Numbers are drawn from
std::lognormal_distribution.- Parameters
shape: shape of resulting xexpressionmean: mean of normal distributionstd_dev: standard deviation of normal distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::cauchy(const S &shape, T a, T b, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a Cauchy random number distribution with peakaand scalebNumbers are drawn from
std::cauchy_distribution.- Parameters
shape: shape of resulting xexpressiona: peak of the Cauchy distributionb: scale of the Cauchy distributionengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::fisher_f(const S &shape, T m, T n, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a Fisher-f random number distribution with numerator degrees of freedom equal tomand denominator degrees of freedom equal tonNumbers are drawn from
std::fisher_f_distribution.- Parameters
shape: shape of resulting xexpressionm: numerator degrees of freedomn: denominator degrees of freedomengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classS, classE= random::default_engine_type>
autoxt::random::student_t(const S &shape, T n, E &engine)¶ xexpression with specified
shapecontaining numbers sampled from a Student-t random number distribution with degrees of freedom equal tonNumbers are drawn from
std::student_t_distribution.- Parameters
shape: shape of resulting xexpressionn: degrees of freedomengine: random number engine
- Template Parameters
T: number type to use
-
template<class
T, classE= random::default_engine_type>
xtensor<typename T::value_type, 1>xt::random::choice(const xexpression<T> &e, std::size_t n, bool replace, E &engine)¶ Randomly select n unique elements from xexpression e.
Note: this function makes a copy of your data, and only 1D data is accepted.
- Return
xtensor containing 1D container of sampled elements
- Parameters
e: expression to sample fromn: number of elements to samplereplace: whether to sample with or without replacementengine: random number engine
-
template<class
T, classW, classE= random::default_engine_type>
xtensor<typename T::value_type, 1>xt::random::choice(const xexpression<T> &e, std::size_t n, const xexpression<W> &weights, bool replace, E &engine)¶ Weighted random sampling.
Randomly sample n unique elements from xexpression
eusing the discrete distribution parametrized by the weightsw. When sampling with replacement, this means that the probability to sample elemente[i]is defined asw[i] / sum(w). Without replacement, this only describes the probability of the first sample element. In successive samples, the weight of items already sampled is assumed to be zero.For weighted random sampling with replacement, binary search with cumulative weights alogrithm is used. For weighted random sampling without replacement, the algorithm used is the exponential sort from Efraimidis and Spirakis (2006) with the
weight / randexp(1)trick from Kirill Müller.Note: this function makes a copy of your data, and only 1D data is accepted.
- Return
xtensor containing 1D container of sampled elements
- Parameters
e: expression to sample fromn: number of elements to samplew: expression for the weight distribution. Weights must be positive and real-valued but need not sum to 1.replace: set true to sample with replacementengine: random number engine
-
template<class
T, classE= random::default_engine_type>
voidxt::random::shuffle(xexpression<T> &e, E &engine)¶ Randomly shuffle elements inplace in xcontainer along first axis.
The order of sub-arrays is changed but their contents remain the same.
- Parameters
e: xcontainer to shuffle inplaceengine: random number engine
-
template<class
T, classE= random::default_engine_type>
std::enable_if_t<xtl::is_integral<T>::value, xtensor<T, 1>>xt::random::permutation(T e, E &engine)¶ Randomly permute a sequence, or return a permuted range.
If the first parameter is an integer, this function creates a new
arange(e)and returns it randomly permuted. Otherwise, this function creates a copy of the input, passes it to- See
shuffle and returns the result.
- Return
randomly permuted copy of container or arange.
- Parameters
e: input xexpression or integerengine: random number engine to use (optional)