nextGaussian Samples The Normal Curve

Random.nextGaussian draws mean-zero unit-variance samples; scale by sigma and shift by mean, or call the two-argument form added in Java 17.

JavaData Integrity

Code

snippet
import java.util.Random;

public class BellCurve {

    public static void main(String[] args) {
        Random rng = new Random(7); // 固定种子:样本序列可复现

        // nextGaussian():均值 0、标准差 1 的正态抽样
        double[] sample = new double[5];
        for (int i = 0; i < sample.length; i++) {
            sample[i] = rng.nextGaussian();
        }

        // 平移与缩放:手工合成任意均值/方差的正态分布
        double mean = 100, sigma = 15;
        double scaled = mean + sigma * sample[0];

        // nextGaussian(mean, sigma)(Java 17+)直接给定参数
        double direct = rng.nextGaussian(mean, sigma);

        System.out.println("raw[0] = " + sample[0]);
        System.out.println("scaled = " + scaled);
        System.out.println("direct = " + direct);
        System.out.println("raw[0] within 3 sigma: " + (Math.abs(sample[0]) <= 3));
    }
}

How the logic is organized

The moving parts are the class BellCurve and the function main.

A single loop drives the file — for (int i = 0; i < sample.length; i++).

What to watch for

  • The toolchain by itself is enough — no dependency resolution happens.

Using it

The main routine’s signature is: public static void main(String[] args)

The run emits 4 labeled sections — raw[0] =, scaled =, direct =, raw[0] within 3 sigma: — one per step.

Key statements

double direct = rng.nextGaussian(mean, sigma);
Random rng = new Random(7); // 固定种子:样本序列可复现
// nextGaussian():均值 0、标准差 1 的正态抽样
double[] sample = new double[5];
double scaled = mean + sigma * sample[0];

Interactive Tools for This Topic

FAQ

What does nextGaussian Samples The Normal Curve print when run?
Run it as a complete program — the output lines carry labels such as `raw[0] =`, `scaled =`, `direct =`.