Triangular distribution on [0,1] with specified mode. Uses inverse transform sampling - very efficient, no rejection needed.
The mode parameter should be in [0,1]. Common choices:
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | mode |
mode of the distribution (should be in [0,1]) |
function triangular_dist(mode) real(wp),intent(in) :: mode !! mode of the distribution (should be in [0,1]) real(wp) :: triangular_dist real(wp) :: u, mode_clipped ! ensure mode is in [0,1] mode_clipped = max(0.0_wp, min(1.0_wp, mode)) u = uniform_random_number() if (u < mode_clipped) then ! left side of triangle triangular_dist = sqrt(u * mode_clipped) else ! right side of triangle triangular_dist = 1.0_wp - sqrt((1.0_wp - u) * (1.0_wp - mode_clipped)) end if end function triangular_dist