I need to generate data, which I should use in following calculations. One part of my data should include 15000 points, which have even distribution inside ellipse(runif in R). Here is the the equation of ellipse: Another part of data should include 10000 points inside rectangle. Here is the equation of rectangle:
How can I do this?
-
2Have you tried anything? I suggest you generate random data within the *extremes* of each shape, then filter those points to include only those within the shapes defined by the actual equation. This means you'll need to generate more random numbers than you need to ensure getting sufficient random data. – r2evans Nov 28 '21 at 21:39
-
@r2evans I have tried to draw ellipse only and from there I already had problems, can you show me example with some code, please? I'm not so good at math – Vorrven Nov 28 '21 at 21:43
-
Plotting data has nothing to do with generating random data. – r2evans Nov 28 '21 at 21:49
-
@r2evans I needed vizualization for better understanding, about generating data, I'm aware only of such code: `c<-runif(10000,1,1000)` I don't know how to match this with ellipse – Vorrven Nov 28 '21 at 21:55
2 Answers
Uniformly Distributed Points
Here is an example for the eclipse data (you could follow similar idea for the rectangle one)
n <- 1.5e4
feclipse <- function(x, y) 2 * (x - 6)^2 + 3 * (y - 4)^2 - 10
res <- c()
repeat {
if (length(res) / 2 == n) break
x <- runif(1, 6 - sqrt(5), 6 + sqrt(5))
y <- runif(1, 4 - sqrt(10 / 3), 4 + sqrt(10 / 3))
if (feclipse(x, y) <= 0) {
res <- rbind(res, c(X = x, Y = y))
}
}
and plot(res)
gives
Non-uniformly Distributed Points
Another option is using analytical solution of y
in terms of x
to produce random tuples
x <- runif(n, 6 - sqrt(5), 6 + sqrt(5))
y <- runif(n, 4 - sqrt((10 - 2 * (x - 6)^2) / 3), 4 + sqrt((10 - 2 * (x - 6)^2) / 3))
plot(x, y)
which gives
Or, you can use polar representation to generate the points
theta <- runif(n, 0, 2 * pi)
rho <- runif(n)
x <- rho * sqrt(5) * sin(theta) + 6
y <- rho * sqrt(10 / 3) * cos(theta) + 4
plot(cbind(x, y))
which gives

- 96,636
- 9
- 24
- 81
-
1*Nice*. I particularly like the contrast of uniform-vs-nonuniform distributions. – r2evans Nov 29 '21 at 12:58
-
-
1@Vorrven I think the answer by jblood94 https://stackoverflow.com/a/70156321/12158757 provided a very good one for uniform points in the rectangle. – ThomasIsCoding Dec 02 '21 at 20:12
-
These can both be sampled without rejection using transformed uniform variates.
Rectangle:
set.seed(94)
u <- runif(1.5e4, max = 3)
v <- runif(1.5e4, max = 2)
x <- u + v - 10
y <- v - u + 8
# sanity check
range(x + y)
#[1] -1.999774 1.999826
range(x - y + 15)
#[1] -2.999646 2.999692
plot(x, y)
Ellipse (see Algorithm: Calculate pseudo-random point inside an ellipse):
phi <- runif(1e4, max = 2*pi)
rho <- sqrt(runif(1e4))
x <- sqrt(5)*rho*cos(phi) + 6
y <- sqrt(10/3)*rho*sin(phi) + 4
# sanity check
range(2*(x - 6)^2 + 3*(y - 4)^2)
#[1] 0.001536582 9.999425234
plot(x, y)
Update with derivation:
Rectangle:
The rectangle is bounded by the lines:
y = -2 - x
y = 2 - x
y = x + 12
y = x + 18
The left-most point of the rectangle is found by point where the first and last lines intersect (-10, 8).
The dimensions of the rectangle are 3*sqrt(2) and 2*sqrt(2), so sample from a rectangle with these dimensions:
u = 3*sqrt(2)*U1
v = 2*sqrt(2)*U2
where U1 and U2 are standard uniform random variates (i.e., runif
).
Since the slopes of the bounding lines are -1 and 1, we can think of the rectangle as being rotated -90° about the origin axes. Rotate u and v by -90° to orient the rectangle containing (u, v) to be in the same direction as the desired rectangle:
x = u*cos(-90°) - v*sin(-90°) = sqrt(2)*(u + v)/2 = 3*U1 - 2*U2
y = v*cos(-90°) + u*sin(-90°) = sqrt(2)*(v - u)/2 = 3*U1 + 2*U2
The left-most point of (x, y) is still at the origin, so translate the points by (-10, 8) to match the desired rectangle:
x = 3*U1 - 2*U2 - 10
y = 3*U1 + 2*U2 + 8
Ellipse:
The x - 6
and y - 4
mean your ellipse is centered at (6, 4).
Center the ellipse at the origin by ignoring the 6 and 4, then write the equation for the ellipse in standard form:
x^2/a^2 + y^2/b^2 = 1
a = sqrt(5)
b = sqrt(10/3)
a and b are the dimensions of your ellipse.

- 10,340
- 1
- 10
- 15
-
Can you explain, please, in rectangle how do you calculate x and y? Why do you use digits 10 and 8? – Vorrven Dec 02 '21 at 20:53
-
One more question: can you also explain calculations for ellipse with sqrt and digits 6 and 4? – Vorrven Dec 02 '21 at 21:11
-
1I was afraid you would ask that. I gave it my best in the updated answer, but the derivation is probably a better question for https://math.stackexchange.com/ – jblood94 Dec 02 '21 at 22:27
-
Sorry for disturbing you again. If I have, for example, |x|<=20 and |y|<=20 then will be such calculations right: `u <- runif(500, max = 20) v <- runif(500, max = 20) x <- u + v - 20 y <- v - u` ? – Vorrven Dec 03 '21 at 23:33
-
`x <- runif(500, -20, 20); y <- runif(500, -20, 20)` or `df <- setNames(as.data.frame(matrix(runif(1000, -20, 20), ncol = 2)), c("x", "y"))` – jblood94 Dec 04 '21 at 00:45
-
Also, note from `?runif`: "runif will not generate either of the extreme values...", so you're actually getting |x|<20 and |y|<20, and the same goes for the original post. – jblood94 Dec 04 '21 at 00:47