Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random number for triangular distribution

Subject: Random number for triangular distribution

From: Partha Roy

Date: 28 Sep, 2012 06:32:08

Message: 1 of 10

I want generate random numbers from a triangular distribution. Can anyone help me?
Thanks in advance

Subject: Random number for triangular distribution

From: Bruno Luong

Date: 28 Sep, 2012 06:46:07

Message: 2 of 10

"Partha Roy" <p_roy_007@yahoo.com> wrote in message <k43g98$ksh$1@newscl01ah.mathworks.com>...
> I want generate random numbers from a triangular distribution. Can anyone help me?
> Thanks in advance

% function that generates triangular distribution between (0,1)
trand = @(varargin) 0.5*(rand(varargin{:})+rand(varargin{:}));

% Test
r=trand(1,100000);
hist(r,30)

% Bruno

Subject: Random number for triangular distribution

From: Partha Roy

Date: 28 Sep, 2012 06:58:07

Message: 3 of 10

Thanks Bruno Luong, but may be i was not specific what i wanna know. I want to determine random numbers from a triangular distribution like (5,7,10). how can i write the program for this in the M file. Could you help me with this?
Thanks again.

Subject: Random number for triangular distribution

From: Bruno Luong

Date: 28 Sep, 2012 21:44:08

Message: 4 of 10

Asymmetrical triangular distribution is more complicated to generated:

p = [5 7 10];

c = (p(2)-p(1))/(p(3)-p(1));
a = (p(3)-p(1))*(p(2)-p(1));
b = (p(3)-p(1))*(p(3)-p(2));
l = @(r) r <= c;
f = @(r) p(1) + sqrt(a*r);
g = @(r) p(3) - sqrt(b*(1-r));
h = @(r) l(r).*f(r) + (1-l(r)).*g(r);
trand = @(varargin) h(rand(varargin{:}));

r = trand(1,100000);
hist(r,30)

% Bruno

Subject: Random number for triangular distribution

From: Roger Stafford

Date: 29 Sep, 2012 06:09:17

Message: 5 of 10

"Partha Roy" <p_roy_007@yahoo.com> wrote in message <k43hpv$plh$1@newscl01ah.mathworks.com>...
> ..... I want to determine random numbers from a triangular distribution like (5,7,10).
- - - - - - - - -
  Here's a slightly different method:

 n = 100000;
 a = 5; b = 10; c = 7;
 r = c+sqrt(rand(n,1)).*(a-c+rand(n,1)*(b-a));

Roger Stafford

Subject: Random number for triangular distribution

From: Bruno Luong

Date: 29 Sep, 2012 07:01:13

Message: 6 of 10

"Roger Stafford" wrote in message <k463ad$dm5$1@newscl01ah.mathworks.com>...
>
> n = 100000;
> a = 5; b = 10; c = 7;
> r = c+sqrt(rand(n,1)).*(a-c+rand(n,1)*(b-a));
>

Brilliant!

Bruno

Subject: Random number for triangular distribution

From: Partha Roy

Date: 29 Sep, 2012 10:43:08

Message: 7 of 10

What can I say Roger... Mind Blowing :)

Subject: Random number for triangular distribution

From: Bruno Luong

Date: 29 Sep, 2012 10:56:09

Message: 8 of 10

Might I ask you Roger what leads you to this formula?

You don't need to elaborate the calculation details, just explaining the overall approach will satisfy me. Thanks.

Bruno

Subject: Random number for triangular distribution

From: Roger Stafford

Date: 29 Sep, 2012 11:35:08

Message: 9 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k46k48$7s3$1@newscl01ah.mathworks.com>...
> Might I ask you Roger what leads you to this formula?
> You don't need to elaborate the calculation details, just explaining the overall approach will satisfy me. Thanks.
> Bruno
- - - - - - - - - -
  The triangular distribution f(x|a,b,c) as defined in Wikipedia:

 http://en.wikipedia.org/wiki/Triangular_distribution

is the same as the distribution of the x-coordinates of a statistically uniform distribution of (x,y) points in the interior of the triangle (a,1),(b,1),(c,0), and that is what my method is based on. The y-coordinates would be given by the sqrt(rand(n,1)) factor.

  This approach admittedly has the disadvantage of requiring twice as many calls on 'rand' to achieve the same result as yours, Bruno, though it has fewer lines of code.

Roger Stafford

Subject: Random number for triangular distribution

From: Bruno Luong

Date: 29 Sep, 2012 14:49:05

Message: 10 of 10

"Roger Stafford" wrote in message <k46mdc$equ$1@newscl01ah.mathworks.com>...
>
> is the same as the distribution of the x-coordinates of a statistically uniform distribution of (x,y) points in the interior of the triangle (a,1),(b,1),(c,0), and that is what my method is based on. The y-coordinates would be given by the sqrt(rand(n,1)) factor.
>

Got it. Thanks.

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us