massisenergy
massisenergy

Reputation: 1820

Random sets with three random numbers in it (sampling random points in a cube)

Each point inside a cube has three values (X, Y & Z axes). There are in-built functions in R, pertaining to the generation of sets with single random number (one number per observation), such as runif(), sample(), rnorm(), set.seed() etc. Thinking in a similar logic, the numbers are generated in a single axis, with those functions.

My question is:

Upvotes: 1

Views: 460

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

Considering the case of cubes, runif is perfectly flexible to achieve all that. In particular, we can specify the number of points to be generated, and for each axis we may also specify a different range. For instance,

lower <- c(0, 10, 20)
upper <- c(1, 11, 21)
n <- 5
matrix(runif(n * 3, lower, upper), ncol = 3, byrow = TRUE)
#            [,1]     [,2]     [,3]
# [1,] 0.03372777 10.99940 20.03487
# [2,] 0.33839128 10.91506 20.61724
# [3,] 0.28628535 10.73780 20.83405
# [4,] 0.31427078 10.49257 20.69737
# [5,] 0.64146235 10.64392 20.97785

The same would hold for rnorm, rbeta, etc.

Thinking about this differently, you want to sample from a multivariate distribution, where perhaps we even have some dependence. For that there also are functions, such as ?mvrnorm in the MASS package or ?rdirichlet in MCMCpack. However, when dealing, say, with points distributed uniformly in a cube, following the approach above is standard and, if there is a need, you may define a corresponding function for the multivariate uniform distribution with independent components.

Upvotes: 4

Related Questions