[p5.js 2.0+ Bug Report]: randomGaussian() leaks its spare value across p5 instances, breaking seeded determinism

Offen
#9,130 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

@iUjjwalRaj arbeitet bereits daran.

Seit 05.10.2026.

  • #9247 von @iUjjwalRaj — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
38/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
javascript
Bereich
frontend

Rechercherichtung

Beginne in src/math/random.js und untersuche den bestehenden übersprungenen Unabhängigkeitstest für den Instanzmodus. Reproduziere die abwechselnden Aufrufe mit gesetztem Seed mit zwei p5-Instanzen und führe anschließend die relevanten Zufallszahlentests aus. Die Aufgabe ist abgeschlossen, wenn die Instanzen unabhängige deterministische Sequenzen erzeugen und der Regressionstest erfolgreich ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Most appropriate sub-area of p5.js?

Math

p5.js version

2.x main (4b096e2)

Actual vs expected behavior

The randomSeed() reference promises that a constant seed "makes these functions produce the same results each time a sketch is run", explicitly naming randomGaussian(). That breaks as soon as two p5 instances exist on a page.

randomGaussian() uses the Marsaglia polar method, which produces values in pairs. The flag that says "a spare value is cached" lives on the instance (this._gaussian_previous), but the spare value itself lives in module scope (let y2 = 0; in src/math/random.js). With two instances, B's spare overwrites A's, and A's next call returns B's value.

Executed repro (both instances seeded, one interleaved call on instance B):

A alone      : -0.2050, -0.3410, 0.3986, -1.4309
A interleaved: -0.2050, -0.9404, 0.3986, -1.4309

Same seed, different sequence. The existing test suite even contains a skipped "instance mode / should be independent" block asserting exactly this independence. PR #1681 (2015) made _gaussian_previous per-instance but left y2 module-scoped, which is the remaining half of that fix.

Steps to reproduce

Outputs above are from executed runs against main, two instances sharing the prototype as in instance mode.

Note

I have a fix ready (store the spare as this._gaussian_y2) with a regression test, mutation-tested against main. Filing for approval first per the contributing guide; will open the PR once approved.

Vorherrschende Sprache
JavaScript
Sterne
24.3k
Forks
3.9k
Ø Merge
3 T. 18 Std.
Gemergte PRs (30 T.)
45

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus processing/p5.js

Alle Issues in processing/p5.js

Ähnliche Issues

Weitere Issues zu JavaScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.