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.
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
- 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.
- 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.
- 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:
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, and . • Compute:
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 is a uniform random variable, then is an exponentially distributed random variable with rate parameter .
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
- Generate random numbers with logarithmic distribution and custom slope
- Generate random permutation of huge list in Python
- Generating a gaussian distribution with only positive numbers
- Generating all 5 card poker hands
- Generating All Combinations of List n Levels Deep in Java
- Generating all factors of a number given its prime factorization
- Generating all permutations excluding cyclic rotations
- Generating all permutations of a given string

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 courseTrack 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.