I want to generate six Random numbers $a,b,c,d,e,f$ (all are real and positive). They are related by $a^2+b^2+c^2+d^2+e^2=1$ and $f$ is between 0 and 3. How can I generate those numbers?

  • 4
    $\begingroup$ Append[Normalize[RandomReal[1, 5]], RandomReal[3]]? $\endgroup$ – J. M. will be back soon May 14 '16 at 14:45
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 May 14 '16 at 14:48
  • $\begingroup$ @J.M. your suggestion is not working. $\endgroup$ – Arpan May 14 '16 at 14:48
  • $\begingroup$ What version of Mathematica are you using? $\endgroup$ – J. M. will be back soon May 14 '16 at 14:49
  • $\begingroup$ I am using Mathematica 9 $\endgroup$ – Arpan May 14 '16 at 14:54

Letting the 5-vector $ x = \{a^2, b^2, c^2, d^2, e^2 \} = \{x_1, x_2, x_3, x_4, x_5\}$ have DirichletDistribution[{1,1,1,1,1}], RandomVariate[DirichletDistribution[{1,1,1,1,1}]] gives a random 4-vector that satisfies $ 0 \leq x_i \leq 1$ and $\sum_{i=1}^4 x_i <1$. The 5th component of $x$ is determined by the condition $x_5= 1 - x_1 - x_2 - x_3 - x_4$. So appending $1 - \sum_{i=1}^4 x_i$ to $\{x_1, x_2, x_3, x_4\}$ returned by RandomVariate[DirichletDistribution[{1,1,1,1,1}]] and taking Sqrt we get a random vector $\{a, b, c, d, e \}$ that satisfies the condition.

Combining the steps in a function:

rvF[dim_Integer, ss_: 1] := Sqrt[Append[#, 1 - Total[#]] & /@ 
    RandomVariate[DirichletDistribution[ConstantArray[1, dim]], ss]];



{{0.866565, 0.266605, 0.134535, 0.307307, 0.255831}}


Mathematica graphics

To get a sample of size 10 for the 6-vector, $\{a, b, c, d, e , f \}$ with f distributed uniformly on [0,3]:

Join @@@ Transpose[{rvF[5, 10], List /@ RandomReal[3, {10}]}]

Mathematica graphics

Further examples:

cp = ContourPlot3D[ x^2 + y^2 + z^2 == 1, {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
   Mesh -> None, ContourStyle -> Directive[Yellow, Opacity[0.7]]];
Show[cp, ListPointPlot3D[rvF[3, 1000]]]

Mathematica graphics


To settle this: Append[Normalize[RandomReal[1, 5]], RandomReal[3]] produces a set of numbers that satisfies the OP's request. What distribution this tuple follows is a different kettle of fish.

  • $\begingroup$ If I am reading OP correctly @bob, he only wants the first 5 to satisfy the normalization condition, and the sixth to be within $(0,3)$. Normalize[] will map the sixth number back to $(0,1)$. $\endgroup$ – J. M. will be back soon May 14 '16 at 15:06
  • 2
    $\begingroup$ If we want to sample uniformly from the set of all tuples that satisfy the constraint, we could do Normalize /@ Select[RandomReal[1, {100, 5}], 0.1 < Norm[#] < 1 &]. The upper bound on the norm ensures (five-dimensional) spherical symmetry. The lower bound avoids numerical instability when Normalizeing. This has to be repeated until a sufficient number of tuples are generated. Since this is rejection sampling, we don't know in advance how many will pass the test. $\endgroup$ – Szabolcs May 14 '16 at 15:12
  • $\begingroup$ @Szabolcs, yes, one alternative would be to use something like a While[] loop to return the first admissible tuple from the rejection sampling. $\endgroup$ – J. M. will be back soon May 14 '16 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.