Sling Academy
Home/Rust/Generating Random Distributions: Normal, Poisson, Uniform in Rust

Generating Random Distributions: Normal, Poisson, Uniform in Rust

Last updated: January 03, 2025

In the world of programming, the ability to generate random numbers and particularly random distributions is often critical in tasks such as simulations, modelling, and algorithm design. Rust, known for its safety and performance, offers various libraries that make this task straightforward. In this article, we’ll walk through generating random distributions, including normal, Poisson, and uniform distributions, using Rust.

Setting Up the Environment

To begin generating random numbers in Rust, we need to add rand crate to our Cargo.toml file. This crate provides multiple functionalities, including random distributions:

[dependencies]
rand = "0.8"

After adding the rand crate, ensure you have cargo installed, then run cargo build in the terminal to download and compile the dependencies.

Generating a Uniform Distribution

A uniform distribution is one of the simplest forms of distribution, where each number within a certain range has an equal probability of being picked.

use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let uniform_number: f64 = rng.gen_range(0.0..1.0);
    println!("Uniformly distributed number: {}", uniform_number);
}

In the above example, we use gen_range() to generate a floating-point number within the range [0.0, 1.0). The thread_rng() function provides us with a random number generator that is local to the current thread.

Generating a Normal Distribution

A normal distribution is probably the most common type of statistical distribution, often called a Gaussian distribution. It is characterized by its bell-shaped curve.

use rand_distr::{Normal, Distribution};

fn main() {
    let normal_dist = Normal::new(0.0, 1.0).unwrap();
    let v = normal_dist.sample(&mut rand::thread_rng());
    println!("Normally distributed number: {}", v);
}

Here, we utilize the rand_distr crate, which is a part of the rand family but needs to be included in your dependencies like so: rand_distr = "0.4". The Normal::new(0.0, 1.0) call constructs a normal distribution with mean 0 and standard deviation 1.

Generating a Poisson Distribution

A Poisson distribution typically expresses the probability of a given number of events occurring in a fixed interval of time or space.

use rand_distr::{Poisson, Distribution};

fn main() {
    let poisson_dist = Poisson::new(4.0).unwrap();
    let v = poisson_dist.sample(&mut rand::thread_rng());
    println!("Poisson distributed number: {}", v);
}

The above implementation samples a number from a Poisson distribution with a lambda (average rate of occurrence) of 4. We use thread_rng() as our random number generator, and Poisson::new(4.0) sets up our distribution.

Conclusion

Through crate rand and rand_distr, Rust provides an efficient and comprehensive way of generation various types of random distributions. Whether you need uniform, normal, or Poisson distributions, Rust's ecosystem supports your requirements with performance and safety. The examples provided can serve as a basis for more complex applications involving random distributions.

Next Article: Working with Probability Functions and Statistical Tests in Rust

Previous Article: Implementing Vector and Matrix Libraries for Rust Game Development

Series: Math and Numbers in Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior