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.

initial_state

(arrayish)
RNG state (ui64[2]).

x

(arrayish)
The population to sample from, a 1-D array.

Value

(list() of arrayish)
List of two elements: the updated RNG state and the sampled values, of shape shape and with the data type of x.

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

state <- nv_rng_state(42L)
pop <- nv_array(c(10, 20, 30))
result <- nv_sample(5, state, pop)
result[[2]]
#> AnvlArray
#>  20
#>  30
#>  10
#>  30
#>  10
#> [ CPUf32{5} ]