# sample_with_replacement()

Sample items from a list with replacement.

## Synopsis

```sample_with_replacement(list-expression, expression)
sample_with_replacement(list-expression, expression, weight-list-expression)
```

## Description

This function returns a list of values randomly sampled from the first argument with replacement. The number of samples is specified by the second argument.

An optional, weight list can be specified in order to set a normalized probability for the correspdonding index in the sample pool list being drawn. For example, a weight list: `list(1, 0, 1)` will have a 50% chance that the element with index 0, will be selected, a 50% chance that the element with index 2 will be selected, and a 0% chance that the element with index 1 will be selected.

## Parameters

`list-expression`

An expression that evaluates to the list to select from.

`expression`

An expression that evaluates to the number of samples to return

`weight-list-expression`

An expression that evaluates to the list of weights to be used in the sampling.

## Returns

The function returns a list. The returned list is empty if the first argument evaluates to an empty list or if the second argument evaluates to 0.

## Examples

Example of an un-weighted sampling:

```x_list = list(1, 2, 3, 4, 5, 6, 7)
sample_list = sample_with_replacement(x_list, 4)
# possible result: sample_list = list(5, 2, 7, 2)
```

Example of a weighted sampling:

```x_list = list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

weight_list = apply(x_list, pow(_, 2))
# weight_list = list(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)

sample_list = sample_with_replacement(x_list, 4, weight_list)
# possible result: sample_list = list(10, 9, 7, 9)
```

## Note

Each call to the function returns a distinct random sample.