Tutorials › Introduction to Statistics › Generating Random Outcomes With Software

Simulation and Probability Models · Tutorial 377 of 996

Generating Random Outcomes With Software

Generate and inspect software random numbers, understanding how a uniform model describes their values and what a finite sequence can—and cannot—show.

Beginner 10 min read

What You'll Learn

  • Define the continuous uniform model on the interval from zero to one.
  • Explain why equal-length intervals have equal probability under this model.
  • Generate a sequence with common spreadsheet and programming tools.
  • Check generated values against the software’s stated range.
  • Rescale uniform values to a continuous interval when the model calls for it.
  • Recognize why a short sequence cannot establish that a generator is truly random.

Why Generate Random Numbers?

A simulation needs a way to produce chance-like inputs. Spreadsheet and programming software provide random-number generators for this purpose. Their output is often a sequence of decimal values between zero and one. Those decimals are not, by themselves, outcomes such as “win” or “lose.” They are numerical inputs that can be used to represent chance according to a model.

To use them responsibly, it helps to distinguish the mathematical ideal from the computer operation. The ideal is a sequence of independent values drawn from a uniform distribution. A computer instead produces values by applying an algorithm. A well-designed generator is intended to make its output behave much like the ideal for ordinary simulation work, but the output is not literally a fresh physical random event each time.

Definition: A random variable \(U\) has the continuous uniform distribution on \([0,1)\), written \(U\sim\operatorname{Uniform}(0,1)\), if its probability is spread evenly across that interval. In particular, for \(0\leq a\leq b\leq 1\), $$ P(a\leq U<b)=b-a. $$ An ideal sequence of uniform random numbers consists of independent values with this distribution.

The formula says that probability is determined by interval length. For example, an interval from 0.20 to 0.35 has length 0.15, so an ideal uniform value has probability 0.15 of falling in that interval. The interval from 0.70 to 0.85 has the same length and therefore the same probability. The location of an interval does not matter; its length does.

This model is a convenient standard input for simulation. It does not claim that the decimal values seen on a screen are equally likely in every practical sense, nor that a short list will look perfectly balanced. A computer has only finitely many representable values and uses an algorithm. For introductory simulations, the generator’s output is treated as an approximation to independent uniform draws, with the software’s documented output range in mind.

Generating a Sequence

In common spreadsheet software, entering =RAND() in a cell returns a value at least 0 and less than 1. Copying the formula down a column requests additional values. In Python, random.random() returns a value in the same range; calling it repeatedly produces a sequence. Other software may use a different function name, so check its documentation for the range and behavior.

Spreadsheet formulas such as RAND may recalculate when the sheet changes or recalculates. As a result, the values on display can change. A programming call obtains values as the program runs. In either setting, what matters at this stage is that you can generate a sequence and check whether its values meet the range promised by the software. Managing repeatable runs is a separate issue.

Worked Example: Generating and Checking Spreadsheet Values

Suppose a spreadsheet user enters =RAND() in cells A1 through A6. For illustration, assume the displayed sequence is 0.14, 0.82, 0.37, 0.05, 0.69, and 0.44. These are hypothetical output values, not a claim about what a particular spreadsheet run will produce.

Check the documented range. Each value must be at least 0 and less than 1. Here the smallest value is 0.05, which is above 0, and the largest is 0.82, which is below 1. All six values satisfy the stated range.

Inspect the sequence. The values are not in increasing order: 0.82 is followed by 0.37, and then 0.05. That is not an error. Randomly generated values are not supposed to sort themselves into an orderly pattern. The sequence has six entries because the formula was copied into six cells.

Interpret cautiously. The output is compatible with the software’s stated range, but six values cannot demonstrate that a generator is independent or uniform. This check identifies an obvious range problem; it is not a statistical certification of the generator.

A sequence is an ordered list, so its order is part of what you inspect. Do not sort it before checking whether the displayed order appears unusual: sorting necessarily removes the original order. Nor should you expect the values to alternate between small and large, or to contain the same number of values in every part of the interval. Such expectations confuse a long-run model with the irregularities of a finite sequence.

Worked Example: Calling a Generator in Python

A student wants five illustrative uniform inputs in a Python program. A basic approach is to import the random module and call its random function five times. Suppose the program displays the hypothetical list 0.073, 0.614, 0.928, 0.301, and 0.487.

Check the number of outputs. The list contains five values, matching the five requested calls. The values are in the order in which the calls produced them.

Check the range. Each value is at least 0 and less than 1. For instance, 0.073 is positive and 0.928 is below 1; the remaining values also lie inside the stated range.

Decide what the inspection supports. The list has no value outside the documented interval and no repeated value. Neither fact proves that the sequence is random: repetitions are possible in an ideal sequence, and a nonrandom sequence could also happen to have no repeats. The check supports only the limited conclusion that these outputs look compatible with the range and do not reveal an obvious defect.

What Uniform Means: A Short Justification

The uniform model can be understood through its density. On the interval from 0 to 1, its density is 1, and outside that interval its density is 0. Probability over an interval is the area under the density there. Since the density is flat at height 1, the area is simply interval length times 1.

$$ P(a\leq U<b)=\int_a^b 1\,du=b-a, \qquad 0\leq a\leq b\leq 1. $$

For instance, taking \(a=0.20\) and \(b=0.35\) gives \(P(0.20\leq U<0.35)=0.35-0.20=0.15\). The same reasoning gives probability 0.15 for any other interval of length 0.15 that lies within the unit interval.

Because this is a continuous model, the probability of any one exact value is zero: an interval consisting of a single point has length zero. A computer, however, can output only from a finite set of representable numbers. The model is an approximation used to describe the generator’s behavior, not a literal statement that a computer can produce infinitely many possible decimals.

Using Uniform Values on Another Continuous Scale

Uniform values can also be rescaled to a different continuous interval. If \(U\) is uniform on \([0,1)\), then \(Y=a+(b-a)U\) lies on the interval from \(a\) up to, but not including, \(b\), for \(a<b\). The transformation stretches the unit interval by the factor \(b-a\), then shifts it by \(a\). This is useful only when a uniform model on the target interval is appropriate for the chance question.

Worked Example: Rescaling to a Time Interval

Imagine a hypothetical model in which a time is assumed to be uniformly distributed from 5 minutes up to, but not including, 9 minutes. Suppose a generator produces the values 0.12, 0.50, and 0.93. To convert each value to minutes, use \(Y=5+(9-5)U=5+4U\).

For \(U=0.12\), \(Y=5+4(0.12)=5+0.48=5.48\) minutes. For \(U=0.50\), \(Y=5+4(0.50)=5+2=7.00\) minutes. For \(U=0.93\), \(Y=5+4(0.93)=5+3.72=8.72\) minutes.

Check the result. Since all three inputs are at least 0 and less than 1, each transformed value is at least 5 and less than 9 minutes. The transformation preserves their order because multiplying by 4 and adding 5 both preserve order. The example shows how to change a continuous scale; it does not show that real waiting times are necessarily uniform.

Common Mistakes and Limits of Inspection

  • Expecting exact balance in a small sequence. A learner may reason, “There are six values, so there should be three below 0.5 and three above it.” The uniform model does not require that balance in every short run. A finite sequence can have more values on one side simply by chance.
  • Assuming that a value in range proves randomness. Values between 0 and 1 can be made by a fixed nonrandom rule. Range checking is necessary but is not enough to establish independence or uniform behavior.
  • Treating a displayed decimal as infinitely precise. A screen shows a rounded representation. For example, 0.37 may stand for a more precise internal value. Rounding can also make distinct internal values look identical, so displayed repeats do not by themselves reveal how the generator works.
  • Confusing the ideal model with the software implementation. The ideal uniform variable is continuous, while software computes a finite sequence. For ordinary introductory work, a standard generator is used as an approximation to the model, not as a guarantee of physical randomness.
  • Using a uniform model without a reason. Generating uniform values is a way to supply simulated inputs. It does not establish that a real quantity, such as a waiting time, is uniformly distributed. The chance model must fit the situation.

A careful inspection therefore has a modest but useful purpose. Confirm the software function, the promised range, the number of values requested, and the sequence as displayed. If the values fall outside the documented range, investigate the formula or code. If they fall inside it, proceed with the intended model while remembering that visual inspection of a short sequence is not a proof of independence or uniformity.

Key takeaway: A software random-number generator supplies a sequence intended to approximate independent values from a uniform model. Check what range the software promises and verify the output, but do not expect a short sequence to be perfectly balanced or treat a plausible appearance as proof of randomness.

Check Your Understanding

Answer these questions using the distinction between the ideal model and a finite software output.

  1. Under the continuous uniform model on \([0,1)\), what is the probability that \(U\) falls between 0.40 and 0.65? Explain using interval length.
  2. A spreadsheet returns 0.08, 0.73, 0.73, and 0.91. Is the repeated value alone evidence that the generator is faulty? Why or why not?
  3. A programming function is documented to return values at least 0 and less than 1. What would you check in a short output list, and what would that check not establish?
  4. If \(U=0.30\) is rescaled to the interval from 10 to 20 using \(Y=10+(20-10)U\), what is \(Y\), including units if the interval represents seconds?
  5. Why is it incorrect to expect exactly half of every short sequence to fall below 0.5?