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.
Code
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
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.Passphrase GeneratorGenerate memorable multi-word passphrases from a fixed 150-word list with crypto randomness, separators, and capitalization options.Password GeneratorGenerate random passwords from selectable character classes using crypto.getRandomValues with rejection sampling and class guarantees.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
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 =`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.