From d95e0b2438caaced3de3d9ab6912db4cfb930aad Mon Sep 17 00:00:00 2001 From: pr0m1th3as Date: Sun, 18 Feb 2024 02:47:14 +0200 Subject: [PATCH] fix truncated random in PiecewiseLinear distribution classdef --- inst/dist_obj/PiecewiseLinearDistribution.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/inst/dist_obj/PiecewiseLinearDistribution.m b/inst/dist_obj/PiecewiseLinearDistribution.m index 0e100135..5959c6fa 100644 --- a/inst/dist_obj/PiecewiseLinearDistribution.m +++ b/inst/dist_obj/PiecewiseLinearDistribution.m @@ -350,7 +350,16 @@ function disp (this) error ("random: requires a scalar probability distribution."); endif if (this.IsTruncated) - r = plrnd (this.Truncation(1), this.Truncation(2), varargin{:}); + lp = plcdf (this.Truncation(1), this.x, this.Fx); + up = plcdf (this.Truncation(2), this.x, this.Fx); + u = unifrnd (lp, up, varargin{:}); + r = zeros (size (u)); + [~, bin] = histc (u(:)', this.Fx); + r0 = this.x(bin); + dx = diff (this.x); + dF = diff (this.Fx); + dr = (u(:)' - this.Fx(bin)) .* dx(bin) ./ dF(bin); + r(:) = r0 + dr; else r = plrnd (this.x, this.Fx, varargin{:}); endif