# A uniform way to generate a random element based on a given probability distritution function, a random number genarator and a given element interval?

Dispatching is one of Julia’s strong suits. I was wondering, is there a single function which would generate a
random element based on a given probability distribution function, a random number genarator and a given element interval? I’m using Julia 0.7. If not, what are your suggestions?

I’m talking about something similar to the fit_mle(D, x, w) method in the DIstribution.jl.

`Distributions.fit_mle` — Method.

``````fit_mle(D, x, w)
``````

Fit a distribution of type `D` to a weighted data set `x` , with weights given by `w` .

Here, `w` should be an array with length `n` , where `n` is the number of samples contained in `x` .

source

### Applicable distributions

The `fit_mle` method has been implemented for the following distributions:

Univariate:

Multivariate:

1 Like

For univariate distributions, that’s pretty much what `rand` does, when you truncate distributions, see

https://juliastats.github.io/Distributions.jl/latest/truncate.html

For multivariate distributions, there is no general efficient algorithm for ensuring that the result is withing a given interval. You will have to come up with something specialized, or iterate until the result is in the box, but that may be horribly inefficient if the probability mass there is small.

4 Likes