I have a distribution defined in a particular domain of the variable but now I want to generate a random variable not in the entire domain but only in a subset of the domain. Here is what I'm trying to do

pdf= ProbabilityDistribution[1/Cos[x]^2, {x, -a, a}, Method -> "Normalize"];

This will generate a random variable from the given distribution between (-a,a). But I want to generate the variable between some subset, say (-a/2,a/2). How do I do this?

I have tried changing the domain of the distribution itself but it is easy to see that this will redefine the whole distribution and is not the same as what I want.


1 Answer 1


Take your start:

a = 0.25*\[Pi];
pdf = ProbabilityDistribution[1/Cos[x]^2, {x, -a, a}, 
   Method -> "Normalize"];

Create a truncated distribution:

tpdf = TruncatedDistribution[{-a/2, a/2}, pdf];

Get the Min/Max of a bunch...

MinMax@RandomVariate[tpdf, 1*^6]





  • 1
    $\begingroup$ Thanks! this is very straightforward. $\endgroup$
    – user51833
    Jun 17, 2021 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.