Learn R Programming

pracma (version 0.8.6)

polar: Random Points in Unit Circle (Matlab Style)

Description

Generate uniformly random points in the unit circle (or in a circle of radius r).

Usage

polar(n = 1, r = 1)

Arguments

n
number of points, default 1.
r
radius of circle, default 1.

Value

  • A pair of values representing a point in the circle, or a matrix of size (n, 2).

Details

Generates tuples of points and rejects those outside the circle.

Examples

Run this code
U <- polar(1000, 2)
plot(U[, 1], U[, 2], pch="+")

#-- v is 2 independent normally distributed elements
# u <- polar(1); r <- t(u) %*% u
# v <- sqrt(-2 * log(r)/r) * u

n <- 5000; U <- polar(n)
R <- apply(U*U, 1, sum)
P <- sqrt(-2 * log(R)/R) * U  # rnorm(2*n)
hist(c(P))

Run the code above in your browser using DataLab