LLM is Like a Box of Chocolates: the Non-determinism of ChatGPT in Code Generation

Shuyin Ouyang, Jie M. Zhang, Mark Harman, Meng Wang · DOI

Abstract

Large Language Models (LLMs) like ChatGPT are increasingly integrated into software engineering workflows. However, their inherent non-determinism—the tendency to produce varying outputs for identical prompts—is often overlooked. This study systematically quantifies this instability across semantic, syntactic, and structural dimensions using 829 coding problems. Our findings reveal that even at a temperature of zero, ChatGPT produces inconsistent results, significantly impacting software reliability and the reproducibility of academic research.

1. Introduction

In the current landscape of AI-assisted software development, LLMs are frequently treated as deterministic tools. However, because these models predict tokens based on probability distributions, the same prompt can yield wildly different code snippets across multiple requests. This inconsistency poses a threat to software reliability; a developer might receive a functional solution in one instance and a buggy one the next for the same problem. Furthermore, this non-determinism creates a "reproducibility crisis" in research, where reported performance gains may actually fall within the model's natural variance. This paper seeks to quantify the extent of this instability in ChatGPT.

2. Methodology

The authors conducted an empirical study using GPT-3.5 and GPT-4, making five separate requests for each coding problem using identical prompts. The resulting code candidates were evaluated across three primary lenses:

  1. Semantic Similarity: Measured via test pass rates and the Output Equivalence Rate (OER), which tracks the ratio of identical test outputs across candidates.
  2. Syntactic Similarity: Assessed using Longest Common Subsequence (LCS) and Levenshtein Edit Distance (LED) to compare literal code text.
  3. Structural Similarity: Evaluated by comparing Abstract Syntax Trees (AST) to determine if the underlying logic (loops, variables, flow) remained consistent.
Experimental framework for evaluating non-determinism across semantic, syntactic, and structural dimensions.

The study also investigated the influence of temperature settings, prompting strategies (e.g., Chain-of-Thought), and task difficulty on output stability.

3. Experimental Setup

The study utilized 829 coding problems from three major benchmarks: CodeContests (competitive programming), APPS (introductory to interview level), and HumanEval (unit-testing problems).

Summary of the three benchmark datasets used in the study.

Additionally, a meta-analysis of 76 recent LLM-based code generation papers was performed to assess how the research community accounts for non-determinism.

4. Key Findings

4.1 The Myth of Temperature Zero

While setting the temperature parameter to 0 is often recommended for determinism, the study found that ChatGPT still produces varying code for the same prompt. This persistent instability may stem from hardware-level floating-point errors or internal architectural complexities like sparse Mixture of Experts.

4.2 Complexity and Randomness

Counter-intuitively, longer and more detailed problem descriptions were found to increase non-determinism. Complex constraints appear to confuse the model, leading to higher variance in the generated logic rather than a more specific, stable answer.

4.3 Semantic and Syntactic Divergence

Under default settings, the ratio of tasks where five generated programs produced zero identical test outputs was alarmingly high: 75.76% for CodeContests and approximately 50% for APPS and HumanEval. In nearly 40% of HumanEval problems, the maximum difference in pass rates for the same prompt reached 1.00, indicating total inconsistency. Syntactic similarity was also low, with a mean LCS of 0.22 for CodeContests.

4.4 The Research Gap

The meta-analysis revealed that only 21.1% of the 76 surveyed papers considered non-determinism in their evaluations. This suggests that a significant portion of current LLM research may report results that are not consistently reproducible.

5. Discussion and Conclusion

The study highlights a critical need for awareness regarding LLM instability. While GPT-4 shows slightly more determinism than GPT-3.5 at default temperatures, both models remain unstable at temperature 0. Future work should focus on mitigation strategies such as voting mechanisms and repair loops. The authors also call for LLM providers to offer customizable levels of determinism to support both creative exploration and production-grade stability.

Summary updated

BibTeX
@article{ouyang2025llm,
  author = {Shuyin Ouyang and Jie M. Zhang and Mark Harman and Meng Wang},
  title = {{LLM is Like a Box of Chocolates: the Non-determinism of ChatGPT in Code Generation}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2025},
  doi = {10.1145/3697010},
}
Figure