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, initial_state, x)

Arguments

shape

(integer())
Shape of the result.

initial_state

(arrayish)
RNG state: a 1-D array of two ui64 elements, as nv_rng_state() returns. The data type and length are fixed by the generator, not by the default data types, and the returned state has them too.

x

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

Value

(named list of two arrayish)
Elements state, the updated RNG state, and values, the sample of shape shape and x's data type.

See also

nv_sample_int() to sample the integers 1 to n.

Other rng: nv_normal, nv_rbinom(), nv_rng_state(), nv_runif(), nv_sample_int()

Examples

# the sample takes the population's data type
state <- nv_rng_state(42L)
pop <- nv_array(c(10, 20, 30))
result <- nv_sample(5, state, pop)
result$values
#> AnvlArray
#>  20
#>  30
#>  10
#>  30
#>  10
#> [ CPUf32{5} ]