Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   Ryan Turner <>
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.


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'

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

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:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index