'Is it possible to sample from user defined distribution in Rust?

I am very new to Rust. I want to sample from a Maxwell-Boltzmann distribution using the rand crate. There may be a way to use other distributions not already in rand, but I cannot seem to work out how. Does anyone know if this is possible?



Solution 1:[1]

To create a custom distribution, you need to implement Distribution for your type.

There is another crate, rand_distr, which implements Distribution for many common probability distributions, and you could use any of those as an example of what to do.

For example this is the implementation of the Poisson distribution.

You might consider contributing your Maxwell-Boltzmann implementation to the rand_distr crate when you are finished! :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1