Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Return (triangle) distribution as -gen var = rtriangle()-

 From Ryan Turner To statalist@hsphsun2.harvard.edu Subject st: Return (triangle) distribution as -gen var = rtriangle()- Date Tue, 8 Jan 2013 15:11:38 +0000

```Dear Statalist:

I am trying to generate a (triangle) distribution for use in the same form as the other standard distributions, e.g.:

set obs 100
gen myuni = runiform()

I have written the following program below to do so, but it returns 'triangle not found'.  I understand there is a difference between a user defined program (series of stata commands) and (user-defined?) functions e.g. for use with egen.  I think I need to return a matrix, I'm not sure.

The best I could do with the manual was from -help egen-: "Here fcn() is a function specifically written for egen, as documented below or as written by users"; but nothing regarding user-defined functions was documented below.

Best,
Ryan

capture program drop triangle
program define triangle
syntax [, a(real 0.0) b(real 1.0) c(real 0.5)]

local U = runiform()

if `U' < (`c'-`a')/(`b'-`a') {
local x = `a' + sqrt(`U'*(`b'-`a')*(`c'-`a'));
}
else {
local x = `b' - sqrt((1-`U')*(`b'-`a')*(`b'-`c'));
}

return `x'
end

. set obs 100
. gen mytri = triangle, a(10) b(20) c(15)

--
Ryan J. Turner <rjturner at cmu dot edu>
Engineering and Public Policy
Carnegie Mellon University
+1-484-483-3244 | +351-961-205-923

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```