Skip to contents

Samples elements of a 1-D array with equal probability and with replacement, analogous to R's sample().

Unlike R's sample(), x is always the population itself: sampling the integers 1 to n is nv_sample_int() and never an overload of x.

Usage

nv_sample(shape, x)

Arguments

shape

(integer())
Shape of the result.

x

(arrayish)
The population vector to sample from. Must not be empty. An R value materializes at its default data type.

Value

(arrayish)
The sample, of shape shape and x's data type.

Random State

The draws come from the active random state: the global random state (see nv_set_seed()), or the random state that with_nv_rng_state() or local_nv_rng_state() make the default for a scope. They advance it in place.

Examples

# the sample takes the population's data type
with_nv_seed(42L, nv_sample(5, nv_array(c(10, 20, 30))))
#> AnvlArray
#>  10
#>  20
#>  30
#>  20
#>  30
#> [ CPUf32{5} ]