# Random number generation from a power law

**URL:** https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701
**Category:** General Usage
**Tags:** question, package
**Created:** [June 22, 2023, 1:11pm UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701 "2023-06-22T13:11:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ABCD](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@ABCD](https://discourse.julialang.org/u/ABCD)
#### Post date: [June 22, 2023, 1:11pm UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/1 "2023-06-22T13:11:34Z")

</div>

hello all,  
I am new to Julia. I need to generate random numbers from power law, broken power law and different distributions. Is there a package that do all of these? Thank you very much in advance.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 22, 2023, 2:56pm UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/2 "2023-06-22T14:56:43Z")

</div>

Welcome to the discussion board. 🙂  
I have not used this, but perhaps [Distributions.jl](https://juliastats.org/Distributions.jl/stable/) might work for you?

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [June 22, 2023, 6:29pm UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/3 "2023-06-22T18:29:55Z")

</div>

More precisely, here [https://juliastats.org/Distributions.jl/stable/univariate/#Sampling-(Random-number-generation)](https://juliastats.org/Distributions.jl/stable/univariate/#Sampling-(Random-number-generation)) you can find the univariate distributions and sampling from an arbitrary one.  
Example:

```julia
using Distributions, Plots
plotlyjs()
const α = 2.75
const xmin= 3.0
d = Pareto(α, xmin)
smpl= rand(d, 1000)
h=histogram(smpl, bins= xmin:50, normalize=:pdf, 
            framestyle=:box, size=(500, 250), legend=false)

```

 ![histPareto](https://global.discourse-cdn.com/julialang/original/3X/1/9/19ba1a7f0a687b9636eb75dbe5c88670131dfd4e.png)

Broken power law distribution is not implemented in Distributions.jl. It depends on the number of break points and power indices on each interval.  
I only know of the existence of the broken power law, implemented in astropy: [https://docs.astropy.org/en/stable/\_modules/astropy/modeling/powerlaws.html](https://docs.astropy.org/en/stable/_modules/astropy/modeling/powerlaws.html), but without a sampling method.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [June 22, 2023, 8:49pm UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/4 "2023-06-22T20:49:52Z")

</div>

Don’t know much about broken power laws, but mixture models might be able to piece them together:

```julia
alpha_below = 1.6
alpha_above = 1.2
breakpoint = 5.4
p_below = cdf(Pareto(alpha_below), breakpoint)
broken_dist = MixtureModel([truncated(Pareto(alpha_below), nothing, breakpoint),
                            truncated(Pareto(alpha_above), breakpoint, nothing)],
                           [p_below, 1 - p_below])
rand(broken_dist)

```

---

<div class="post-metadata">

### Author: ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)
#### Post date: [June 23, 2023, 8:30am UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/5 "2023-06-23T08:30:04Z")

</div>

If your distribution is known and sufficiently smooth\[1\] you can use [ApproxFun.jl](https://juliaapproximation.github.io/ApproxFun.jl/latest/generated/Sampling/).

* * *

1. Though in practice 2M Chebyshev modes is enough to approximate some very wonky functions.

---

<div class="post-metadata">

### Author: ![cgarling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgarling/32/29278_2.png) [@cgarling](https://discourse.julialang.org/u/cgarling)
#### Post date: [June 27, 2023, 10:53am UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/6 "2023-06-27T10:53:12Z")

</div>

I implemented broken-power-law sampling in [InitialMassFunctions.jl](https://github.com/cgarling/InitialMassFunctions.jl), which is now a registered package. This functionality is provided by the `BrokenPowerLaw` type. This type implements the Distributions.jl [`sampler` API](https://juliastats.org/Distributions.jl/stable/extends/#Distributions.sampler-Tuple%7BDistribution%7D), so a more efficient object for sampling can be constructed via `Distributions.sampler(::BrokenPowerLaw)`. You may also be interested in the single power-law `PowerLawIMF` type, which is just a wrapper on top of the Pareto distribution from [Distributions.jl](https://juliastats.org/Distributions.jl/stable/univariate/#Distributions.Pareto).

---

<div class="post-metadata">

### Author: ![ABCD](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@ABCD](https://discourse.julialang.org/u/ABCD)
#### Post date: [July 16, 2023, 7:09am UTC](https://discourse.julialang.org/t/random-number-generation-from-a-power-law/100701/7 "2023-07-16T07:09:15Z")

</div>

All of this is really helpful. Thanks a lot!
