ran_round: Random Rounding of Numbers

View source: R/helpers.R

ran_roundR Documentation

Random Rounding of Numbers

Description

[Stable]

A number x is rounded to integer y according to the following rule:

y = \left\lfloor{x}\right\rfloor + I(u < (x - \left\lfloor{x}\right\rfloor)),

where function I:\{TRUE, FALSE\} \to \{0, 1\}, is defined as:

I(x) = \begin{cases} 0, & x \text{ is } FALSE \\ 1, & x \text{ is } TRUE, \end{cases}

and u is number that is generated from Uniform(0, 1) distribution.

Usage

ran_round(x)

Arguments

x

(numeric)
a numeric vector.

Value

An integer vector.

Examples

x <- c(4.5, 4.1, 4.9)
set.seed(5)
ran_round(x) # 5 4 4
set.seed(6)
ran_round(x) # 4 4 5

stratallo documentation built on Nov. 27, 2023, 1:07 a.m.