Random data: generate random samples for simulation

Updated

Random data are values drawn by the computer from a probability distribution you choose: 100 values from a normal distribution with mean 50 and standard deviation 2, say, or 30 counts from a Poisson distribution with mean 1.5. Because you know exactly what process produced them, they are the ideal test bed: you can see what pure chance looks like, check how often a method gives a false alarm, rehearse an analysis before the real data arrive, or teach a concept with as many samples as you like.

When to use it

  • Teaching and intuition. Show how much a histogram, a mean or a standard deviation varies from sample to sample even when the process does not change.
  • Checking a method. Simulate a stable process to see how often a control chart, a test or a capability index gives a misleading answer.
  • Planning. Try a sample size or a sampling plan on simulated data before committing to it; see also how many samples you need.
  • Monte Carlo studies. Combine random part dimensions to estimate how often an assembly will fall out of tolerance.
  • Random selection. Pick which parts to inspect, or randomize the run order of an experiment.

For regular sequences such as 1, 2, 3 or repeated factor levels, use patterned data instead.

How it works

Uniform numbers. A pseudo-random number generator produces a long sequence of numbers between 0 and 1 that pass statistical tests of randomness. The sequence is completely determined by its starting value, the seed. Same seed, same generator, same software version: same numbers, every time. Without a seed, the generator starts from something like the clock, and each run differs.

Any other distribution. The most general method is the inverse transform: draw a uniform number u and return the value of the distribution that has probability u below it.

x = F⁻¹(u)

For the normal distribution, F⁻¹ is the inverse normal CDF, NORMINV(u, mean, sd). For a discrete distribution, such as the binomial or the Poisson, return the smallest k whose cumulative probability F(k) is at least u. Libraries often use faster special-purpose methods, but the result has the same distribution.

Reproducibility. Record the seed, the generator and the software version with any simulation you report. The same seed in a different program, or a different version of the same one, usually gives different numbers.

Example

The inverse transform by hand. Six uniform numbers turn into six values from a normal distribution with mean 50 and standard deviation 2 through NORMINV(u, 50, 2), the same function that StatLab's calculator and super calculator provide:

u0.5120.0870.9340.2710.6550.990
x = NORMINV(u, 50, 2)50.0647.2853.0148.7850.8054.65

A small u gives a value in the lower tail, u near 0.5 a value near the mean, and u = 0.990 a value 2.33 standard deviations above it. For a Poisson count with mean 1.5 and u = 0.40: POISSONCDF(0, 1.5) = 0.223 is below 0.40 and POISSONCDF(1, 1.5) = 0.558 is above it, so the count is 1.

What a sample looks like. One hundred values drawn from the same normal distribution (mean 50, standard deviation 2) with a fixed seed have a sample mean of 50.09, a standard deviation of 1.83, and run from 46.14 to 54.51:

100 random values from a normal distribution, mean 50, sd 2010203040Frequency47.550.052.555.0Value
100 random values from a normal distribution, mean 50, sd 2

Even from a perfectly normal process, the histogram is lumpy and the standard deviation came out 8% below the true 2. Keep that in mind before reading too much into the shape of 100 real measurements.

A simulation: false alarms on a control chart. Draw 25 subgroups of 5 from a stable normal process (mean 50, standard deviation 2), build an Xbar chart with limits from the same data (x̿ ± A₂R̄, with A₂ = 0.577), and check whether any subgroup mean falls outside the limits. Repeated 100,000 times with a fixed seed, 6,562 charts, or 6.6%, showed at least one point beyond a limit, although nothing ever changed. Theory agrees: with known limits, each point has a 0.0027 chance of falling outside, so 25 points give 1 − (1 − 0.0027)²⁵ = 6.5%. About one stable chart in 15 will show a Test 1 signal, which is why a single point outside the limits calls for investigation, not panic.

How to read the results

  • Compare sample statistics with the true parameters. The gap is sampling error, the same error your real data carry.
  • Look at many samples, not one. Draw several and see how much histograms, means and standard deviations change.
  • Report simulation results with their precision. A proportion p estimated from N runs has a standard error of √(p(1 − p)/N); for 6.6% from 100,000 runs, that is about 0.08 percentage points.

Common mistakes

  • Not recording the seed. Without it, nobody, including you, can reproduce the numbers.
  • Reusing one seed for runs meant to be independent. They will be identical, not independent.
  • Too few simulation runs. Ten runs of a rare event tell you almost nothing.
  • Simulating the wrong model. Normal random data say nothing about a skewed or drifting real process.
  • Mixing simulated data with real data in the same worksheet without labels.

Frequently asked questions

What is a random seed?

The starting value of a pseudo-random number generator. Setting it makes the sequence repeatable, so a simulation or a random sample can be reproduced exactly.

Are computer-generated random numbers truly random?

No. They are pseudo-random: produced by a deterministic formula, but with statistical properties indistinguishable from true randomness for simulation work. They are not suitable for security purposes.

How do I generate normally distributed random numbers?

Draw uniform numbers between 0 and 1 and transform each with the inverse normal CDF, NORMINV(u, mean, sd). See probability distributions for the other inverse functions.

How many simulation runs do I need?

Enough that the standard error of your answer is small compared with the precision you need. For a probability near 5%, 10,000 runs give a standard error of about 0.2 percentage points; 100,000 runs, about 0.07.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab