Re: [R] generate random numbers subject to constraints

From: Robert A LaBudde <>
Date: Thu, 27 Mar 2008 04:02:07 -0500

At 05:06 PM 3/26/2008, Ted Harding wrote:
>On 26-Mar-08 21:26:59, Ala' Jaouni wrote:
> > X1,X2,X3,X4 should have independent distributions. They should be
> > between 0 and 1 and all add up to 1. Is this still possible with
> > Robert's method?
> >
> > Thanks
>I don't think so. A whileago you wrote
>"The numbers should be uniformly distributed" (but in the
>context of an example where you had 5 variable; now you
>are back to 4 variables). Let's take the 4-case first.
>The two linear constraints confine the point (X1,X2,X3,X4)
>to a triangular region within the 4-dimensional unit cube.
>Say it has vertices A, B, C.
>You could then start by generating points uniformly distributed
>over a specific triangle in 2 dimentions, say the one with
>vertices at A0=(0,0), B0=(0,1), C0=(1,0). This is easy.
>Then you need to find a linear transformation which will
>map this triangle (A0,B0,C0) onto the triangle (A,B,C).
>Then the points you have sampled in (A0,B0,C0) will map
>into points which are uniformly distributed over the
>triangle (A,B,C).
>More generally, you will be seeking to generate points
>uniformly distributed over a simplex.
>For example, the case (your earlier post) of 5 points
>with 2 linear constraints requires a tetrahedron with
>vertices (A,B,C,D) in 5 dimensions whose coordinates you
>will have to find. Then take an "easy" tetrahedron with
>vertices (A0,B0,C0,D0) and sample uniformly within this.
>Then find a linear mapping from (A0,B0,C0,D0) to (A,B,C,D)
>and apply this to the sampled points.
>This raises a general question: Does anyone know of
>an R function to sample uniformly in the interior
>of a general (k-r)-dimensional simplex embedded in
>k dimensions, with (k+1) given vertices?

The method of "rejection":

  1. Generate numbers randomly in the hypercube.
  2. Test to see if the point falls within the prescribed area.
  3. Accept the point if it does.
  4. Repeat if it doesn't.

Efficiency depends upon the ratio of volumes involved.

Robert A. LaBudde, PhD, PAS, Dpl. ACAFS e-mail:
Least Cost Formulations, Ltd.            URL:
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire" mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 27 Mar 2008 - 08:05:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 19:31:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive