Uniform Distribution

The uniform distribution refers to a probability distribution in which all outcomes are equally likely. For example, rolling a fair die produces uniform distribution over numbers 1 to 6.

The distribution is characterized by two parameters - a and b denoting the start and end of the distribution range. All numbers between a and b are equally probable.

Uniform distribution is useful as a simple null hypothesis for probability. It represents complete uncertainty about the distribution.

Solution

Here is how to generate random numbers from a uniform distribution:

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
double[] uniformDist(int n, double a, double b) {
  double[] result = new double[n];
  Random rand = new Random();
  
  for(int i=0; i<n; i++) {
    result[i] = a + rand.nextDouble()*(b-a);
  }

  return result;
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
vector<double> uniformDist(int n, double a, double b) {
  vector<double> result(n);
  default_random_engine generator;
  uniform_real_distribution<double> distribution(a, b); 

  for(int i=0; i<n; i++) {
    result[i] = distribution(generator);
  }

  return result;
}

Python

1
2
3
4
5
6
7
8
9
from random import uniform

def uniform_dist(n, a, b):
  result = []
  for i in range(n):
    num = uniform(a, b)
    result.append(num)
  
  return result

Uniform distribution provides equal probability over a range of values.

Description: Uniform Distribution

Uniform Distribution is a type of probability distribution in which all outcomes are equally likely. In a discrete uniform distribution, each integer value has an equal chance of being picked. In a continuous uniform distribution, any value between the given minimum and maximum is equally likely to occur. It is characterized by two parameters: the minimum value (a) and the maximum value (b).

Solution:

Generating random numbers with a uniform distribution is quite common in programming. Here’s how to do it in Java, C++, and Python.

Java

Java’s Random class can be used to generate uniformly distributed random numbers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.util.Random;

public class UniformDistribution {
    public static void main(String[] args) {
        Random random = new Random();
        int min = 0;
        int max = 10;

        for (int i = 0; i < 10; i++) {
            int randomNum = random.nextInt((max - min) + 1) + min;
            System.out.println(randomNum);
        }
    }
}

C++

C++’s <random> library provides ways to generate uniformly distributed random numbers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
#include <random>

int main() {
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(0, 10);

    for (int i = 0; i < 10; ++i) {
        int randomNum = distribution(generator);
        std::cout << randomNum << std::endl;
    }

    return 0;
}

Python

Python’s random module provides a function to generate uniformly distributed numbers.

1
2
3
4
5
6
7
8
import random

min = 0
max = 10

for _ in range(10):
    randomNum = random.randint(min, max)
    print(randomNum)

Key Takeaways:

  • Uniform distribution is when all outcomes have an equal chance of occurring.
  • Java, C++, and Python all provide built-in methods to generate uniformly distributed random numbers.
  • The range of the distribution is defined by its minimum (a) and maximum (b) values.