[Paper Review] An Empirical Study of the Non-determinism of ChatGPT in Code Generation
This paper empirically demonstrates high non-determinism in ChatGPT’s code generation across CodeContests, APPS, and HumanEval, shows that zero temperature does not guarantee determinism, and analyzes correlations with task features.
There has been a recent explosion of research on Large Language Models (LLMs) for software engineering tasks, in particular code generation. However, results from LLMs can be highly unstable; nondeterministically returning very different codes for the same prompt. Non-determinism is a potential menace to scientific conclusion validity. When non-determinism is high, scientific conclusions simply cannot be relied upon unless researchers change their behaviour to control for it in their empirical analyses. This paper conducts an empirical study to demonstrate that non-determinism is, indeed, high, thereby underlining the need for this behavioural change. We choose to study ChatGPT because it is already highly prevalent in the code generation research literature. We report results from a study of 829 code generation problems from three code generation benchmarks (i.e., CodeContests, APPS, and HumanEval). Our results reveal high degrees of non-determinism: the ratio of coding tasks with zero equal test output across different requests is 75.76%, 51.00%, and 47.56% for CodeContests, APPS, and HumanEval, respectively. In addition, we find that setting the temperature to 0 does not guarantee determinism in code generation, although it indeed brings less non-determinism than the default configuration (temperature=1). These results confirm that there is, currently, a significant threat to scientific conclusion validity. In order to put LLM-based research on firmer scientific foundations, researchers need to take into account non-determinism in drawing their conclusions.
Motivation & Objective
- Quantify the extent of non-determinism in ChatGPT's code generation across multiple benchmarks.
- Assess how temperature affects non-determinism in code generation.
- Compare non-determinism between different prompt configurations (multi-request vs. single-request).
- Explore correlations between coding task features (e.g., instruction length) and non-determinism.
- Provide guidance for researchers to account for non-determinism in empirical software engineering studies.
Proposed method
- Use ChatGPT (gpt-3.5-turbo and gpt-4) to generate five code candidates per coding task across three benchmarks (CodeContests, APPS, HumanEval).
- Prompt for Python3 code with Markdown formatting to extract code from API responses.
- Evaluate semantics via test pass rates, output equivalence rate (OER) and OER excluding exceptions.
- Measure syntactic similarity with LCS and Levenshtein Edit Distance between code candidates.
- Measure structural similarity using AST-based methods (pycode_similar with Unified_Diff and Tree_Diff).
- Investigate the influence of temperature (0, 1, 2) and compare GPT-3.5 vs. GPT-4.

Experimental results
Research questions
- RQ1RQ1: How non-deterministic is ChatGPT in code generation across semantic, syntactic, and structural perspectives?
- RQ2RQ2: How does temperature affect the degree of non-determinism in code generation?
- RQ3RQ3: How does non-determinism compare between five candidates from different requests versus five top-1 candidates from one request?
- RQ4RQ4: What correlations exist between coding task features (e.g., instruction length, difficulty) and non-determinism metrics?
- RQ5RQ5: How does GPT-4's non-determinism compare with GPT-3.5 in code generation?
Key findings
- A high non-determinism threat is observed: not a single equal test output among top-five candidates in 72.73%, 60.40%, and 65.85% of tasks for CodeContests, APPS, and HumanEval respectively.
- Maximum difference in test pass rate across candidates reaches 1.00 for all datasets, with 48.17% of HumanEval problems showing this max difference.
- Zero temperature does not guarantee determinism; however, temperature 0 yields better determinism than temperature 1, while temperature 2 often leads to uniformly worse code.
- Longer coding instructions correlate negatively with syntactic and structural similarity and with average correctness.
- Across surveyed literature, only 21.1%–22.4% of LLM-based code generation studies account for non-determinism in experiments.
- Structural similarity remains relatively high across candidates (United_Diff: CodeContests 0.46, APPS 0.52, HumanEval 0.69; Tree_Diff: CodeContests 0.60, APPS 0.68, HumanEval 0.72).

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.