rand_vect: Random vector of N integers that sum to M

Description Usage Arguments Value Examples

View source: R/rand_vect.R

Description

Random vector of N integers that sum to M (https://stackoverflow.com/questions/24845909/generate-n-random-integers-that-sum-to-m-in-r)

Usage

1
rand_vect(N,M)

Arguments

N

Size of the vector.

M

Sum of elements in the vector.

sd

Approximate standard deviation for elements of vector (default = 1).

pos.only

Resulting vector contains only positive integers (default = TRUE)

Value

Vector of size N with elements summing to M

Examples

1
2
3
4
5
6
7
8
rand_vect(3, 50)
[1] 17 16 17
rand_vect(10, 10, pos.only = FALSE)
[1]  0  2  3  2  0  0 -1  2  1  1
rand_vect(10, 5, pos.only = TRUE)
[1] 0 0 0 0 2 0 0 1 2 0
rand_vect(10, -1, pos.only = FALSE)
[1] -1 -1  1 -2  2  1  1  0 -1 -1

vanderlindenma/lindenutils documentation built on Dec. 23, 2021, 2:10 p.m.