Why Seeds Matter in NLME Simulation
A fitted nonlinear mixed-effects model can generate many layers of simulated quantities: population parameters, subject-level random effects, between-occasion effects, covariates, and residual errors. Each simulated quantity may depend on random-number draws. A seed provides a reproducible starting point for the pseudorandom number generator that supplies those draws. This is useful when checking a result, comparing implementations, or rerunning an analysis after changing code.
Reproducibility is not the same as precision. Running a simulation twice with the same seed can reproduce the same estimate exactly, but that agreement does not show that the estimate is close to its target. Precision depends on Monte Carlo variation and, in general, on the number of appropriately generated replicates. The earlier material on Monte Carlo error and replicate counts provides the context for that distinction; here the focus is how random-number initialization and management affect the run.
A PRNG commonly produces a sequence that behaves like draws from a uniform distribution on \([0,1)\). Simulation algorithms transform these values into draws from other distributions. For instance, a uniform value can select a category, while a normal random effect can be generated by a transformation or algorithm based on one or more pseudorandom values. Consequently, changing the seed can change simulated random effects and outcomes even when the fitted model and simulation code are unchanged.
What a Seed Does—and Does Not—Reproduce
Resetting a seed restarts a pseudorandom sequence from its initialized state. If the generator, its implementation, the seed, and the order and number of random-number requests are all the same, the resulting draws will ordinarily be the same. This is the basic mechanism behind repeatable runs.
The exact result can nevertheless depend on more than the seed. Software versions may use different generator algorithms or transformations; a code change may request random numbers in a different order; and parallel execution may allocate draws differently unless the software provides a reproducible stream strategy. Reproducing a run across machines or software environments may therefore require recording the generator and relevant software versions as well as the seed.
The following distinction is important for a nested simulation. Suppose outer iteration \(m\) draws a parameter vector \(\theta^{(m)}\), and then inner iteration \(r\) draws subject-level random effects and residual errors under that parameter vector. The seed determines the pseudorandom sequence used across these layers, but the simulation design determines how that sequence is consumed and which draws belong to which iteration.
Here \(G_{\theta}\) represents a distribution used for parameter uncertainty when that is part of the simulation; \(\Omega^{(m)}\) is the random-effect covariance matrix associated with the parameter draw; and \(f\) is the structural model evaluated at the specified times and covariates. A simulation may instead hold \(\theta\) fixed. In either case, random numbers may be used at more than one level. A reproducible plan should state which run uses which stream of pseudorandom values, rather than treating a single seed as a complete description of the simulation design.
Worked Example: Resetting a Seed Repeats a Toy Simulation
Consider a deliberately simple, hypothetical simulation. A PRNG initialized with seed 31415 produces the first three uniform values \(0.12\), \(0.73\), and \(0.41\). The simulation records whether each value is below \(0.5\). This is equivalent to generating three Bernoulli outcomes with success probability \(0.5\), using the rule “success if \(U<0.5\).”
The first value, \(0.12\), is below \(0.5\), so the first outcome is a success. The second value, \(0.73\), is not below \(0.5\), so the second outcome is a failure. The third value, \(0.41\), is below \(0.5\), so the third outcome is a success. The result is therefore success, failure, success, with a simulated success proportion of \(2/3=0.667\).
If the same generator is reset to seed 31415 and the same three requests are made, the same three pseudorandom values and outcomes are produced. This demonstrates repeatability for this fixed setup. It does not show that \(0.667\) is a precise estimate of a success probability: there are only three simulated outcomes, and another seed could yield a different proportion.
Managing Seeds Across Nested Simulation Layers
In an NLME simulation, a run may include outer parameter draws and inner simulated populations. There are two general ways to organize random-number use. A single initialized sequence can be consumed in a fixed, documented order across the whole simulation. Alternatively, software with a supported stream or substream facility can assign distinct streams to simulation units, such as outer iterations or parallel workers. Either approach can be reproducible when implemented appropriately.
A frequent error is to initialize the generator to the same seed at the start of every replicate. If every replicate then makes the same random-number requests, it receives the same pseudorandom values. In a fixed-parameter simulation, this can produce duplicate simulated subjects or populations rather than new replicates. In a nested simulation, resetting at each inner iteration can also repeat the same random effects and residual patterns under different parameter draws. Such repetition is not a substitute for independent simulation replicates.
Worked Example: Why Reusing One Seed Per Replicate Fails
Suppose a hypothetical simulation generates a one-subject outcome using two successive uniform draws: the first is transformed to a random effect and the second is used to generate residual error. With seed 2468, imagine the generator returns \(0.20\) and \(0.80\) for those requests. The model’s transformations produce an outcome of \(8.4\) concentration units.
If the program resets the generator to seed 2468 before each of four replicates, each replicate again uses \(0.20\) and \(0.80\). Each produces \(8.4\) concentration units. The four reported values are \(8.4, 8.4, 8.4, 8.4\): they are repeated copies, not four newly generated outcomes. Their apparent lack of variation is an artifact of the reset strategy.
A better design initializes the generator once before the sequence of four replicates and lets it advance, or assigns a distinct documented stream to each replicate using a supported stream mechanism. For example, a single sequential stream might supply pairs \((0.20,0.80)\), \((0.47,0.11)\), \((0.93,0.36)\), and \((0.62,0.71)\). The model transforms each pair separately. The resulting outcomes will generally differ, although the particular values depend on the transformation. The important design feature is that each replicate receives new pseudorandom values rather than restarting at the same state.
This remedy does not mean that any arbitrarily chosen different seed guarantees statistically independent streams. Use the generator’s documented stream or substream functionality when distinct streams are required, especially for parallel work. If that facility is unavailable, a single sequential run with a recorded initialization and fixed execution order is often easier to reproduce than manually assigning seeds without a documented strategy.
Parallel computation needs particular care. If workers share a generator state, or if each worker starts from the same seed, random-number sequences may overlap or be duplicated. A supported parallel random-number method should allocate reproducible streams to workers or tasks. To reproduce results, record the stream-allocation method and task mapping, not merely a top-level seed. Scheduling can otherwise change which task consumes which values.
Identify outer parameter draws, simulated subjects, occasions, and any other layer that consumes random numbers. Decide which quantities are held fixed and which are redrawn.
Use either a single initialized sequence in a fixed order or a documented stream/substream strategy supported by the software. Avoid resetting to one identical seed inside every replicate.
Save the seed, generator type, software and version, simulation code or its version, stream allocation, replicate counts, and relevant execution settings.
Check whether the same setup reproduces the expected outputs. If it does not, investigate changed code, generator behavior, random-number request order, or parallel allocation before interpreting the difference as model uncertainty.
Repeatability Is Not Monte Carlo Precision
For a simulated quantity with finite variance, the Monte Carlo standard error of a sample mean based on \(R\) independent, identically distributed simulation replicates is estimated by \(s/\sqrt{R}\), where \(s\) is the sample standard deviation of the simulated quantity. The approximation relies on the replicates providing appropriate independent draws from the target simulation distribution. A seed is not part of this precision formula: it selects the sequence, while the simulation design and replicate count determine the Monte Carlo information available.
Worked Example: Identical Reruns Do Not Establish Precision
Suppose a hypothetical NLME simulation estimates the probability that a subject’s concentration exceeds a prespecified threshold. Across \(R=400\) appropriately generated replicates, 280 meet the criterion, giving \(\hat{p}=280/400=0.700\). For a simulated binary outcome, the estimated Monte Carlo standard error is
If the same seed and execution setup are rerun, the estimate may again be exactly \(0.700\). That equality verifies repeatability under the setup; it does not reduce the estimated Monte Carlo standard error of approximately \(0.0229\). If the simulation is instead expanded to \(R=1600\) appropriately generated replicates and the estimated proportion remains near \(0.700\), the corresponding approximate standard error is
The reduction in Monte Carlo error comes from having more suitable replicates, not from changing or repeating the seed. The calculation also assumes the simulation replicates are independent for the purpose of this standard-error approximation; duplicated draws caused by resetting the same seed do not satisfy that requirement.
Diagnostics, Alternatives, and Limitations
A seed strategy is going wrong when a supposedly varied simulation produces exact duplicate replicates, when different workers appear to repeat identical sequences, or when a rerun changes despite an unchanged configuration. These observations call for investigation of initialization, stream allocation, random-number request order, and software settings. They are not, by themselves, proof of a particular cause. A compact diagnostic is to compare a few generated random quantities or replicate summaries across a rerun and across replicate indices, while keeping the intended design fixed.
The main alternative to resetting one seed for every replicate is to initialize once and consume a sequential stream. This is straightforward and can be highly reproducible, but changes in code that alter the number or order of random-number requests can shift all later draws. Stream-based methods offer a clearer way to separate tasks and support parallel reproducibility, but depend on a generator and software implementation with documented stream handling. Neither approach removes the need to record the computational setup.
A seed cannot ensure that the fitted NLME model is appropriate, that simulated data follow the intended scientific scenario, or that Monte Carlo error is acceptably small. It also cannot guarantee bit-for-bit reproducibility across different PRNG algorithms, software versions, hardware, or parallel execution settings. The goal is a transparent and repeatable computational experiment, not a claim that the seed itself validates the model or the simulation.
Check Your Understanding
Answer each question, then compare your reasoning with the solutions.
- In precise terms, what does setting a seed allow you to reproduce, and what additional conditions matter?
- Why can resetting the same seed before every inner simulation replicate be problematic?
- Name two items, other than the seed value, that should be recorded to reproduce a simulation run.
- A binary simulation estimate is \(0.25\) from \(R=100\) appropriate replicates. Find its approximate Monte Carlo standard error using \(\sqrt{\hat{p}(1-\hat{p})/R}\).
- Why is a supported stream strategy useful when a nested simulation runs in parallel?
Solutions. (1) A seed initializes a deterministic pseudorandom sequence. Reproduction also depends on the generator and implementation, software environment, code, and the order and number of random-number requests. (2) Identical resets can give every replicate the same sequence, creating duplicated results rather than newly simulated replicates. (3) Examples include generator type and software version, code version, stream allocation, replicate counts, and execution settings. (4) \(\sqrt{0.25(0.75)/100}=\sqrt{0.001875}\approx0.0433\), or about 4.33 percentage points. (5) Documented streams can assign reproducible, distinct sequences to parallel tasks; this helps avoid duplicated or overlapping worker sequences and makes task-level reruns more manageable.