# Best random number generator for Monte Carlo simulations

**URL:** <https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318>\
**Category:** Modelling & Simulations\
**Created:** [May 4, 2023, 1:17pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318 "2023-05-04T13:17:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve1313](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@steve1313](https://discourse.julialang.org/u/steve1313)\
**Post date:** [May 4, 2023, 1:17pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/1 "2023-05-04T13:17:54Z")

</div>

I often write small Monte Carlo simulations to solve difficult probability problems.

What’s the best (fastest, most accurate) RNG for this type of use?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 4, 2023, 1:21pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/2 "2023-05-04T13:21:29Z")

</div>

The best sampling of the input distributions is likely not from any random-number generator, instead, you may want to look into [“low-discrepancy sequences”](https://en.wikipedia.org/wiki/Low-discrepancy_sequence) which result in lower variance than random sampling.

---

<div class="post-metadata">

**Author:** ![steve1313](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@steve1313](https://discourse.julialang.org/u/steve1313)\
**Post date:** [May 4, 2023, 4:49pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/3 "2023-05-04T16:49:48Z")

</div>

Thanks for your reply. That is very interesting. I’ve been wondering about something very similar to that, but did not realize it had already been substantially studied.

Is there a package in Julia that generates quasi-random numbers?

Many of the problems I want to simulate involve randomly drawing a car from a standard deck, one at a time, without replacement, until a certain condition is met, and then stopping; and then repeating that experiment many times to compute the expected value of draws.

Would quasi-random numbers be suitable for simulation like that?

I typically run a billion or more trials when I’m using rand() and get surprisingly accurate results. Would quasi-random numbers run faster and produce more accurate results?

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [May 4, 2023, 6:29pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/4 "2023-05-04T18:29:12Z")

</div>

> [@steve1313](#):
>
> quasi-random

We have [Sobol.jl](https://github.com/stevengj/Sobol.jl) for generating quasi monte carlo experiments but the repository seems a little bit outdated.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 4, 2023, 6:34pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/5 "2023-05-04T18:34:11Z")

</div>

Sobol.jl is great and continues to work without errors on newer versions of Julia.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 12, 2023, 1:01pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/6 "2023-05-12T13:01:29Z")

</div>

Can/should these quasirandom sequences be used as the default go-to choice for all Monte-Carlo simulations, instead of regular PRNGs?  
Like, if I calculate probabilities `mean(_ -> f(rng) > x, 1:10^4)` for a black-box `f(rng)`, should I just replace `rng` with a Sobol sequence generator? Should it improve sampling efficiency in general?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 12, 2023, 1:13pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/7 "2023-05-12T13:13:24Z")

</div>

I think the general idea is that Quasi-Monte Carlo would be a better choice for that, yea. I have also seen mixing, where you do both QMC and regular MC together.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 12, 2023, 1:15pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/8 "2023-05-12T13:15:21Z")

</div>

Does it depend on what `f` actually does with `rng`, or how many numbers does it draw from it?  
If QMC is generally better for this kind of problems, then should it just be used “everywhere” except security and similar stuff?  
I haven’t heard much about QMC before, so maybe these questions seem obvious (:

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 12, 2023, 1:16pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/9 "2023-05-12T13:16:23Z")

</div>

> **[GitHub - SciML/QuasiMonteCarlo.jl: Lightweight and easy generation of...](https://github.com/SciML/QuasiMonteCarlo.jl)**
>
> Lightweight and easy generation of quasi-Monte Carlo sequences with a ton of different methods on one API for easy parameter exploration in scientific machine learning (SciML) - GitHub - SciML/Quas...

Though I’d also say it still needs quite some work, the basics are all there.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 12, 2023, 1:22pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/10 "2023-05-12T13:22:39Z")

</div>

I think it probably does depend what you are doing with it. Also note that you can’t quite “just swap” it out. The Sobol sequences are on the unit cube, so if you may need to transform the draws to fit your randomness (eg invert using the CDF).

Also that SciML package looks great.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 12, 2023, 1:32pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/11 "2023-05-12T13:32:16Z")

</div>

> [@tbeason](#):
>
> … note that you can’t quite “just swap” it out. The Sobol sequences are on the unit cube …

Regular random generators are also on the “unit cube” originally – they generate numbers from 0…1 or integers, that are transformed to whatever is needed. Seems like it’s possible to implement the same Julia RNG interface that’s backed by Sobol sequences, and simply swap RNG with Q-RNG.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 12, 2023, 1:36pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/12 "2023-05-12T13:36:17Z")

</div>

Yes I just wanted to be explicit so that we (and all others) were on the same page. If you were drawing random numbers using `randn`, you cannot just directly swap out for the Sobol sequence!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 12, 2023, 2:48pm UTC](https://discourse.julialang.org/t/best-random-number-generator-for-monte-carlo-simulations/98318/13 "2023-05-12T14:48:15Z")

</div>

> [@jbytecode](#):
>
> We have [Sobol.jl](https://github.com/stevengj/Sobol.jl) for generating quasi monte carlo experiments but the repository seems a little bit outdated.

It still works and is maintained as needed.

For small packages that do one well-defined task, “updated frequently” is not such a reliable heuristic. For such packages, my goal is generally to get it featured-complete and then never have to touch it except to fix bugs or version-compatibility issues.
