Skip to content

Add p5.strands support for randomGaussian()#8800

Open
harshiltewari2004 wants to merge 2 commits into
processing:dev-2.0from
harshiltewari2004:strands-random-gaussian
Open

Add p5.strands support for randomGaussian()#8800
harshiltewari2004 wants to merge 2 commits into
processing:dev-2.0from
harshiltewari2004:strands-random-gaussian

Conversation

@harshiltewari2004
Copy link
Copy Markdown
Contributor

Resolves #8775

Changes

Adds randomGaussian() support for p5.strands, following @davepagurek's guidance in #8775. The implementation is a JS-side wrapper over the existing random() strands function, applying a Box-Muller transform to two uniform samples to produce a normal-distributed sample.

  • New augmentFn entry in src/strands/strands_api.js next to the existing random() registration.
  • Falls through to the original p5 randomGaussian() when called outside a strands shader context.
  • Inside a strands context: builds Box-Muller via two this.random() calls + chained node math.

Implementation note

Uses this.x() rather than fn.x() per @davepagurek's note in the issue thread.

Testing

No unit tests included — there's no existing test/unit/strands/ directory and no clear precedent for unit-testing strands functions (which produce IR nodes rather than directly-testable values). Happy to add tests in whichever direction the maintainers prefer.

Local npm test shows the same 4 flaky visual regression test failures (test/unit/visual/cases/typography.js) that I observed on dev-2.0 before my changes — unrelated to this PR.

PR Checklist

  • npm run lint passes
  • [Inline reference] is included / updated
  • [Unit tests] are included / updated

@perminder-17 perminder-17 self-requested a review May 20, 2026 12:02
@p5-bot
Copy link
Copy Markdown

p5-bot Bot commented May 20, 2026

Continuous Release

CDN link

Published Packages

Commit hash: 055554d

Previous deployments

This is an automated message.

Comment thread src/strands/strands_api.js Outdated
Comment on lines +419 to +431
augmentFn(fn, p5, 'randomGaussian', function(...args){
if(!strandsContext.active){
return originalRandomGaussian.apply(this, args);
}
const mean = args.length >= 1 ? args[0] : 0;
const stdDev = args.length>=2 ? args[1] : 1;

const u1 = this.random();
const u2 = this.random();
const z = this.sqrt(this.log(u1).mult(-2)).mult(this.cos(u2.mult(2*Math.PI)));

return z.mult(p5.strandsNode(stdDev)).add(p5.strandsNode(mean));
});
Copy link
Copy Markdown
Collaborator

@perminder-17 perminder-17 May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the work @harshiltewari2004 , just one concern I have: are you defining randomGaussian inside the random function intentionally? Do you think it should be outside, otherwise randomGaussian won't exist until the user calls random() at least once, and it'll get redefined on every random() call.

Also, can you ad tests for this implementation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks @perminder-17 — fixed in the latest commit. The randomGaussian augmentation is now a sibling of random, not nested inside it.

@harshiltewari2004
Copy link
Copy Markdown
Contributor Author

Happy to add tests @perminder-17 — I noticed there's no test/unit/strands/ directory yet, so I held off. Should I establish that pattern here, or is there an existing test approach for strands I missed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants