random number generation
numerical distributions
probability distributions
statistical sampling
random sampling

Generate random numbers with a given numerical distribution

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Generating random numbers with a specific numerical distribution is a common requirement in various scientific and engineering applications like simulations, statistical sampling, cryptography, and computer graphics. Understanding how to generate such numbers is essential when finite samples need to represent a potentially infinite population with certain statistical properties.

This article explores methods to generate random numbers corresponding to different distributions, discussing both the theoretical underpinnings and practical implementations.

Basic Concepts

  1. Random Number Generators (RNG): These are algorithms generating sequences of numbers that mimic the properties of random variables. The sequences are determined by an initial seed value, making them pseudorandom.
  2. Numerical Distribution: This is a function describing the probability of random variables within a specified range. Common distributions include uniform, normal, binomial, and Poisson distributions.
  3. Transformation Methods: A class of algorithms that transform numbers from a simple distribution (often uniform) to the target distribution.

Generating Random Numbers for Different Distributions

Uniform Distribution

The simplest form is the uniform distribution where every number within a range has an equal chance of being selected.

Method:

• Utilize the built-in random() function available in many programming libraries, which produces numbers in a uniform distribution between 0 and 1. • For arbitrary bounds [a, b], the transformation is given by: X=a+(ba)×random()X = a + (b - a) \times random()

Normal Distribution

Also known as Gaussian distribution, it's essential in statistics thanks to the Central Limit Theorem.

Box-Muller Transform:

• Converts two independent uniform random numbers into two independent standard normally distributed numbers. • Formulation: • Generate two independent uniform random numbers, U1U_1 and U2U_2. • Compute: Z_0=2lnU_1cos(2πU_2)Z\_0 = \sqrt{-2 \ln{U\_1}} \cdot \cos{(2\pi U\_2)} Z_1=2lnU_1sin(2πU_2)Z\_1 = \sqrt{-2 \ln{U\_1}} \cdot \sin{(2\pi U\_2)}

Exponential Distribution

The exponential distribution is often used to model the time between independent events that occur at a constant average rate.

Inverse Transform Sampling:

• If UU is a uniform random variable, then X=λln(1U)X = -\lambda \ln(1 - U) is an exponentially distributed random variable with rate parameter λ\lambda.

Poisson Distribution

Used for modeling the number of events occurring within a given time interval.

Rejection Sampling:

• Repeatedly generate samples from a distribution that envelops the function, accepting samples with a probability proportionate to the desired probability.

Practical Implementation Example: Python

Here's an example of generating random numbers from a normal distribution using the NumPy library in Python.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.