# RandomPoint doesn't seem to work

I define a very simple sphere in 3D as follows:

ℛ = ImplicitRegion[(x - 2)^2 + (y - 3)^2 + (z - 8)^2 == 1, {x, y, z}];


Now when I try to sample some 500 points from it using RandomPoint nothing happens:

points = RandomPoint[ℛ, 500]
(* RandomPoint[
ImplicitRegion[(-2 + x)^2 + (-3 + y)^2 + (-8 + z)^2 == 1, {x, y,
z}], 500] *)


What's the problem?

• For a sphere one could go with RandomPoint[Sphere[{2, 3, 8}, 1], 500] – corey979 Apr 24 '18 at 15:09
• @corey979 Your suggestion seems more straightforward than any of the current answers, and should probably be an answer itself. Perhaps you could also compare its speed with other approaches? – MarcoB Apr 24 '18 at 16:07
• Please, be advised that the answer by @corey979 is the appropriate one if you want to sample the sphere with points distributed uniformly – José Antonio Díaz Navas Apr 24 '18 at 17:14
• @MarcoB I suspect that the OP used this simple example to illustrate the problem, but in fact asks for something more general (Henrik's answer seems to be general enough), and because it's so straightforward, I will refrain from making it an answer. – corey979 Apr 24 '18 at 19:23
• Interestingly, RandomPoint[ImplicitRegion[x^2 + y^2 == 1, {x, y}]] works, but in 3D it doesn't. Nothing in the documentation (except for a lack of working examples...) indicates it shouldn't work. – corey979 Apr 24 '18 at 19:31

It works if you discretize the region first:

ℛ = DiscretizeRegion[
ImplicitRegion[(x - 2)^2 + (y - 3)^2 + (z - 8)^2 == 1, {x, y, z}],
MaxCellMeasure -> 0.00001
];
points = RandomPoint[ℛ, 500];


The trade-off is that the points do not lie exactly on the sphere; in this example, the is error about 0.01 percent which should be fine for statistical experiments.

• You could then project onto the original region with RegionNearest. Though this won’t necessarily be a uniform sampling, but probably close enough. – Chip Hurst Apr 24 '18 at 16:15

It works if we define a finite-thickness shell:

eps = .001;
p = RandomPoint[
ImplicitRegion[
1 - eps <= (x - 2)^2 + (y - 3)^2 + (z - 8)^2 <= 1 + eps, {x, y,
z}], 1000];


If you make eps smaller it seems to work but becomes extremely slow.

I see this behaviour rather erroneous:

reg = ImplicitRegion[(x - 2)^2 + (y - 3)^2 + (z - 8)^2 == 1, {x, y, z}];
RegionPlot3D[reg]


leads to an empty plot, and

RandomPoint[reg]


RandomPoint[ ImplicitRegion[(-2 + x)^2 + (-3 + y)^2 + (-8 + z)^2 == 1, {x, y, z}]]

On the other hand, a similar construct in 2D works:

reg2 = ImplicitRegion[x^2 + y^2 == 1, {x, y}];
RandomPoint[reg2]


{0.961994, 0.27307}

while

RegionPlot[reg2]


gives an empty plot, too.

The behaviour of RegionPlot3D is explained in the docs:

RegionPlot3D[pred,....]

The predicate pred can be any logical combination of inequalities.

Moreover:

RegionPlot3D initially evaluates pred at a 3D grid of equally spaced sample points specified by PlotPoints. Then it uses an adaptive algorithm to subdivide at most MaxRecursion times, attempting to find the boundaries of all regions in which pred is True.

The same can be found in the docs of RegionPlot. Ok, so we shouldn't expect the plots to work.

BUT:

Recall that RandomPoint[reg] didn't work; consider instead

reg3 = ImplicitRegion[(x - 2)^2 + (y - 3)^2 - (z - 8)^2 == 1, {x, y, z}];


where I only changed the sign before $(z-8)^2$. Then:

RandomPoint[reg3]


{6.81984, 4.05919, 3.16754}

with an error:

RandomPoint::unbndreg: The specified region appears to be unbounded. Appropriate bounds will be automatically computed. Explicit bounds may be specified as a third argument.

which can be easily dealt with by providing the third argument:

RandomPoint[reg3, 1, {{-10, 10}, {-10, 10}, {-10, 10}}]


{{-2.33781, 2.75421, 3.77188}}

Hence, I see no reason why RandomPoint, working for reg3, shouldn't work for reg.

• The behaviour is the same in v10.4 and v11.3. I see it as a bug. If no one proves me wrong, I will add the tag and report to Wolfram. – corey979 Apr 24 '18 at 20:07
• Yeah, that's actually a weird behavior. – Henrik Schumacher Apr 24 '18 at 22:35