Skip to contents

Generates random positive values that sum to 1 by sampling from a Dirichlet distribution with all concentration parameters equal to 1, i.e. Dirichlet(1, ..., 1). This corresponds to a uniform distribution over the simplex.

Usage

rsum1(n, digits = NULL)

Arguments

n

integer. Number of components (must be >= 1).

digits

optional integer. If provided, the result is rounded to the specified number of decimal places. A correction is applied to ensure the sum remains exactly 1.

Value

Numeric vector of length n with non-negative entries summing to 1.

Details

Optionally, the result can be rounded to a specified number of digits while preserving the total sum of 1.

Values are generated via normalized Gamma draws: $$x_i = g_i / \sum_j g_j, \quad g_i \sim \Gamma(1, 1)$$

If digits is specified, rounding may introduce small numerical deviations. These are corrected by adjusting the largest component, which minimizes relative distortion and avoids negative values.

Examples

rsum1(5)
#> [1] 0.05456093 0.22805884 0.11592740 0.01780025 0.58365259
rsum1(5, digits = 2)
#> [1] 0.37 0.08 0.24 0.14 0.17
rsum1(1)
#> [1] 1