Java Program for Standard Normal Distribution (SND) ZScore and nextGaussian (original) (raw)
Last Updated : 6 May, 2025
In Java, the **Standard Normal Distribution (SND) is a special case of the normal distribution. It occurs when a normal random variable has a **mean of 0 and a **standard deviation of 1. The normal random variable of a standard normal distribution is called a **standard score or a **z-score. A conversion from normally distributed to standard normally distributed values occurs via the formula.
**Formula:
Z = (X - u) / s
**where,
- **Z: Value on the standard normal distribution
- **X: Value on the original distribution
- **u: Mean of the original distribution
- **s: Standard deviation of the original distribution
Why Use the Standard Normal Distribution?
With the help of the Standard Normal Distribution, we can **compare different datasets, even if they have different averages. We can do this by converting values into Z-scores. A Z-score tells how far a data point is from the average, using standard deviation as a measure. For example, if a Z-score is +2, then it simply means the value is 2 standard deviations above the average. If it is -1, then it simply means the value is 1 standard deviation below the average. It becomes easier for us to understand where a data point stands compared to the rest of the data.
**Note: Z-scores are useful in things like testing, analyzing data, and drawing conclusions from statistics.
Java Program to Calculate Z-Score
Now, we are going to calculate the Z-score using the formula we have discussed above.
**Example:
Java `
// Java program to demonstrate the naive method // of finding Z-value import java.io.; import java.util.;
class Geeks { public static void main(String[] args) { // initialization of variables double Z, X, s, u; X = 26; u = 50; s = 10;
// formula
Z = (X - u) / s;
System.out.println("The Z-value obtained is: " + Z);
}}
`
Output
The Z-value obtained is: -2.4
Generating Random Numbers Using the Standard Normal Distribution
In Java, the **nextGaussian() method from the **Random class is used to generate the random numbers, normally distributed double value with mean 0.0 and standard deviation 1.0.
**Declaration of nextGaussian() method:
public double nextGaussian()
- **Parameter: This method does not take any parameter.
- **Return Type: The method call returns the random, Normally distributed double value with mean 0.0 and standard deviation 1.0.
Now, we are going to discuss how to use nextGaussian() method to generate a random value from the standard normal distribution.
**Example:
Java `
// Java program to demonstrate the working // of nextGaussian() import java.util.*;
public class Geeks {
public static void main(String[] args)
{
// create random object
Random r = new Random();
// generating integer
double n = r.nextGaussian();
// Printing the random Number
System.out.println("The next Gaussian value generated is: " + n);
}}
`
Output
The next Gaussian value generated is : -0.9494975084124126
Applications of Standard Normal Distribution
The application of standard normal distribution are listed below:
- **Z-Tests in Hypothesis Testing: The Z-test uses the Z-score to determine if a sample mean differs from the population mean by comparing the Z-score.
- **Confidence Intervals: It helps to calculate the margin of error for sample data.
- **Data Normalization: Used in machine learning to scale data so that all features contribute equally to the model.