Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

randI

Random Integers of Specified Number of Digits


Description

Create n random integer valued numbers all with a specified number of digits d.

Usage

randI(n, d)

Arguments

n

numeric sample size, i.e., length() of result.

d

a positive integer, giving the exact number of digits the resulting numbers must have.

Details

This is based on runif() and not sample(), which for now also makes it less R version dependent.

Value

A numeric vector of length n of numbers N where each N has exactly d digits; equivalently,

10^(d-1) <= N[i] < 10^d,

and every N[i] appears with the same probability 1 / (9*10^(d-1))

Author(s)

Martin Maechler

See Also

Uniform random numbers runif; Random number generators, seeds, etc: RNG.

Examples

plot(
 T2 <- table(randI(1e6, 2))) ; abline(h = 1e6 / (9*10^(2 - 1)), lty=2, col="gray70")
chisq.test(T2) # typically not at all significant
T3 <- table(randI(1e6, 3))
chisq.test(T3)
stopifnot(exprs = {
  identical( 10:99 , as.integer(names(T2)))
  identical(100:999, as.integer(names(T3)))
})

round

Rounding to Decimal Digits

v0.20-0
AGPL (>= 3) | file LICENSE
Authors
Martin Maechler [aut, cre] (<https://orcid.org/0000-0002-8685-9910>), R-core [ctb] ("r1.C" only)
Initial release
2021-01-04

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.