rand.RdCreate random matrices or random points in a unit circle (Matlab style).
rand(n = 1, m = n)
randn(n = 1, m = n)
randi(imax, n = 1, m = n)
randsample(n, k, w = NULL, replacement = FALSE)
rands(n = 1, N = 1, r = 1)
randp(n = 1, r = 1)integers specifying the size of the matrix
integer or pair of integers
number of elements to return.
weight vector, used for discrete probabilities.
logical; sampling with or without replacement.
dimension of a shere, N=1 for the unit circle
radius of circle, default 1.
rand(), randn(), randi() create random matrices of
size n x m, where the default is square matrices if m is
missing.
rand() uses the uniform distribution on ]0, 1[, while
randn() uses the normal distribution with mean 0 and standard
deviation 1.
randi() generates integers between imax[1] and imax[2]
resp. 1 and imax, if imax is a scalar.
randsample() samples k elements from 1:n, with or
without replacement, or returns a weighted sample (with replacement),
using the weight vector w for probabilities.
rands() generates uniformly random points on an N-sphere in
the N+1-dimensional space. To generate uniformly random points in the
N-dim. unit cube, take points in S^{N-1} und multiply with
unif(n)^(1/(N-1)).
randp() generates uniformly random points in the unit circle (or in
a circle of radius r).
Matrices of size nxm resp. a vector of length n.
randp() returns a pair of values representing a point in the circle,
or a matrix of size (n,2). rands() returns a matrix of size
(n, N+1) with all rows being vectors of length 1.
The Matlab style of setting a seed is not available; use R style
set.seed(...).
Knuth, D. (1981). The Art of Computer programming; Vol. 2: Seminumerical Algorithms; Chapt. 3: Random Numbers. Addison-Wesley, Reading.
rand(3)
#> [,1] [,2] [,3]
#> [1,] 0.39051038 0.80696061 0.7997181
#> [2,] 0.96338209 0.06311467 0.5046987
#> [3,] 0.08714915 0.49472290 0.4342131
randn(1, 5)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.7067769 -0.265663 1.892616 -1.360009 0.8478133
randi(c(1,6), 1, 10)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 5 3 6 5 5 2 2 1 5 2
randsample(10, 5, replacement = TRUE, w = c(0,0,0, 1, 1, 1, 1, 0,0,0))
#> [1] 4 4 7 6 4
P <- rands(1000, N = 1, r = 2)
U <- randp(1000, 2)
if (FALSE) { # \dontrun{
plot(U[, 1], U[, 2], pch = "+", asp = 1)
points(P, pch = ".")} # }
#-- v is 2 independent normally distributed elements
# u <- randp(1); r <- t(u) %*% u
# v <- sqrt(-2 * log(r)/r) * u
n <- 5000; U <- randp(n)
R <- apply(U*U, 1, sum)
P <- sqrt(-2 * log(R)/R) * U # rnorm(2*n)
if (FALSE) { # \dontrun{
hist(c(P))} # }