Gaussian Distribution

The Gaussian or normal distribution refers to a probability distribution that looks like a bell curve, symmetric about the mean.

It is characterized by two parameters - mean and standard deviation.

Based on the central limit theorem, the normal distribution occurs frequently in statistics as the distribution of sample means.

It provides a good model for quantities that cluster around a central value, like IQ scores or sample averages.

Solution

Here is code to generate normally distributed random numbers:

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
double[] generateNormals(int n, double mean, double stdDev) {

  double[] result = new double[n];
  Random rand = new Random();

  for(int i=0; i<n; i++) {
    double u1 = rand.nextDouble();
    double u2 = rand.nextDouble();
    double z = Math.sqrt(-2*Math.log(u1))*Math.cos(2*Math.PI*u2);
    result[i] = z * stdDev + mean;
  }

  return result;
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
vector<double> generateNormals(int n, double mean, double stdDev) {

  vector<double> result(n);
  default_random_engine generator;
  normal_distribution<double> distribution(mean, stdDev);

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

  return result;
}

Python

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

def generate_normals(n, mean, std_dev):

  result = []
  for i in range(n):
    num = normalvariate(mean, std_dev) 
    result.append(num)

  return result

Normal distribution provides a good model for many natural phenomena.

Description: Gaussian Distribution

Gaussian distribution, also known as the normal distribution, is a probability distribution that describes how the values of a variable are distributed. It is a bell-shaped curve characterized by its mean (μ) and standard deviation (σ). The mean defines the peak point, and the standard deviation defines the width of the “bell.” In a Gaussian distribution, about 68% of the data falls within one standard deviation of the mean, 95% within two standard deviations, and 99.7% within three standard deviations.

Solution

Here are implementations to generate Gaussian-distributed random numbers in Java, C++, and Python.

Java

In Java, you can use the Random class to generate Gaussian-distributed numbers.

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

public class GaussianExample {
    public static void main(String[] args) {
        Random rand = new Random();
        double mean = 0.0;
        double stdDev = 1.0;

        double randomValue = mean + stdDev * rand.nextGaussian();
        System.out.println("Random Gaussian value: " + randomValue);
    }
}

C++

C++ has a <random> library that can be used to generate Gaussian-distributed numbers.

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

int main() {
    std::default_random_engine generator;
    std::normal_distribution<double> distribution(0.0, 1.0);

    double randomValue = distribution(generator);
    std::cout << "Random Gaussian value: " << randomValue << std::endl;

    return 0;
}

Python

In Python, the random module in the standard library can be used, but a more commonly used library for this is numpy.

1
2
3
4
5
6
7
import numpy as np

mean = 0.0
stdDev = 1.0

random_value = np.random.normal(mean, stdDev)
print("Random Gaussian value:", random_value)

Key Takeaways

  • Gaussian distribution is widely used in statistics and data science.
  • Defined by its mean and standard deviation, the distribution shows how much individual data points deviate from the mean.
  • Java, C++, and Python offer built-in libraries for generating Gaussian-distributed random numbers.
  • Understanding Gaussian distribution is essential for many applications, including machine learning and natural language processing.