question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MathUtils.seededRandom() returned value is linearly correlated to the parameter

See original GitHub issue

Hey,

According to the doc, MathUtils.seededRandom() should return a (pseudo random) float in the interval [0, 1]. However the returned value is linearly correlated to the provided integer parameter: the bigger the seed, the bigger the result.

For instance:

MathUtils.seededRandom(1); // returns 0.000007825903601782307
MathUtils.seededRandom(10); // returns 0.00007826322696941274
MathUtils.seededRandom(100); // returns 0.0007826364606457171
MathUtils.seededRandom(1000); // returns 0.00782636879740876

As far as I know, the results of a prng function shouldn’t be so predictable, but maybe I’m misunderstanding something.

Three.js version: 0.138.0

Cheers

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Mugen87commented, Feb 24, 2022

Okay, let’s give this a try then 👍 .

1reaction
thibkacommented, Feb 24, 2022

Does the current behavior introduce any issues on app level?

Well the current behavior just doesn’t work as it should. So I’m not sure the current seededRandom function could be used in a scenario where a PRNG is needed.

The result of seededRandom() should be a number and not a function in order to keep the usage simple.

That’s true. In that case you could always rewrite the MathUtils.seededRandom function with the mulberry32 algorithm this way:

// Deterministic pseudo-random float in the interval [ 0, 1 ]
function seededRandom( s ) {

	if ( s !== undefined ) _seed = s;

	// Mulberry32 generator

	var t = _seed += 0x6D2B79F5;

	t = Math.imul(t ^ t >>> 15, t | 1);

	t ^= t + Math.imul(t ^ t >>> 7, t | 61);

	return ((t ^ t >>> 14) >>> 0) / 4294967296;

}

The behavior would then be fixed:

THREE.MathUtils.seededRandom(); // 0.6074679309967905 (depends on the previous value of the private _seed variable)
THREE.MathUtils.seededRandom(); // 0.19144689152017236 (depends on the previous value of the private _seed variable)
THREE.MathUtils.seededRandom(1); // 0.6270739405881613 (always)
THREE.MathUtils.seededRandom(10); // 0.5019920116756111 (always)
THREE.MathUtils.seededRandom(100); // 0.2043598669115454 (always)
THREE.MathUtils.seededRandom(); // 0.3148212195374071 (depends on the previous value of the private _seed variable)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Seeding the random number generator in Javascript
I have written a function that returns a seeded random number, it uses Math.sin to have a long random ...
Read more >
A more complete random API? - 💡 Ideas - TC39 - Discourse
Right now, in Javascript, our main source of randomness comes from a single function: Math.random(). I'm wondering if there would be interest in...
Read more >
Stan Modeling Language
This allows adjustment for globally scaled or correlated parameters. Without this adjustment, models with differently scaled parameters ...
Read more >
Math.random() - JavaScript - MDN Web Docs
The Math.random() function returns a floating-point, pseudo-random number that's ... This example returns a random number between the specified values.
Read more >
Stan User's Guide
Stan supports regression models from simple linear regressions to ... where I() is the indicator function taking the value 1 if its argument...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found