I'm trying to get a random solution for an equation with arbitrary number of parameters which has infinite solutions. Note that the answer must be in n dimensions where n is the number of parameters of the function.
My tries:
The function can be something like
x^2 + y^2 + z^2 == 1, in this case, the solution is a sphere and every point that satisfies that equality is a valid solution. This creates a region that consists of valid solutions, so if we generate a random point in that region we have a valid answer for this problem. That can be done using ybeltukov's answer'sRegionDistribution:implregion = ImplicitRegion[x^2 + y^2 + z^2 + t^2 == 1, {x, y, z, t}]; region = DiscretizeRegion[implregion]; pts = RandomVariate[RegionDistribution[region], 100]but this raises an error saying
DiscretizeRegiononly takes arguments with less than 4 dimensions.I tried using
NSolveto get all the points:NSolve[x^2 + y^2 + z^2 == 1, {x,y,z}] (* I don't know what I was supposed to see here *)but this gives a warning message that says:
Warning: NSolve::infsolns: "Infinite solution set has dimension at least 1. Returning intersection of solutions with (171802*x-113492*y-121484*z)/178835 == 1."then it outputs some complex valued solutions, which are not valid (in this context):
{{x->0.252383 - 0.412144I, y->-1.17075 - 0.0795353I, z->-0.0214382 - 0.508549I}, {x->0.252383 + 0.412144I, y->-1.17075 + 0.0795353I, z->-0.0214382 + 0.508549I}}As you can see, it gets those wanted infinite solutions, and it even selects a random solution, but it is always the same, and contains complex numbers when what I want is one random answer of that
solution setwhich consists only of real values.I also tried with
Reducedoing:Reduce[x^2 + y^2 + z^2 == 1]The result is:
(x == -Sqrt[1 - y^2 - z^2] || x == Sqrt[1 - y^2 - z^2])and generating some points doing something like
Table[ {RandomChoice[{-1,1}] Sqrt[1 - y^2 - z^2], y, z}/. {y-> RandomReal[{-1,1}], z-> RandomReal[{-1,1}]}, {1000}]gives me incorrect answers because
x,yandzare not calculated "at the same time".
Edit:
Using
FindInstancedistributes non uniformly the points, I usedFindInstance[x^2 + y^2 + z^2 == 1, {x, y, z}, Reals, 1000]and the result was
And the last method distributes non uniformly too, doing
Select[Table[{RandomChoice[{-1, 1}] Sqrt[1 - y^2 - z^2], y, z} /. {y -> RandomReal[{-1, 1}], z -> RandomReal[{-1, 1}]}, {1000}], Im@#[[1]] == 0 &]gives me
this is because the values are calculated depending on the $yz$ plane and in some places
xchanges faster than in another places.


FindInstance? – mikado Apr 15 '17 at 18:58Table, certainly are correct. What is your concern with them? – bbgodfrey Apr 15 '17 at 19:00RandomPointdo what you're after? – Greg Hurst Apr 15 '17 at 19:53