Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weightedPick sometimes returns undefined #6701

Closed
jameskirkwood opened this issue Dec 30, 2023 · 2 comments
Closed

weightedPick sometimes returns undefined #6701

jameskirkwood opened this issue Dec 30, 2023 · 2 comments

Comments

@jameskirkwood
Copy link

Version

  • Phaser Version: 3.70.0

Description

Phaser.Math.RND.weightedPick sometimes samples the element at index array.length (past the end) and returns undefined. For an array of length 1, this happens about 30% of the time.

Since v3.70.0, the random value before flooring is Math.pow(this.frac(), 2) * array.length + 0.5. The maximum value this can have is (array.length + 0.5). Any value between array.length and array.length + 0.5 will floor to array.length, access past the end and return undefined.

Example Test Code

const size = 1000;
var count = 0;
for (let i = 0; i < size; i++) {
    count += Phaser.Math.RND.weightedPick([1]) !== 1;
}
console.log(count / size);

Also, this example breaks: https://labs.phaser.io/view.html?src=src\tilemap\endless%20map.js&v=3.70.0

Additional Information

Prior to v3.70.0, the distribution of random values before flooring ranged from 0.5 to (array.length - 0.5) which caused the first and last elements to be undersampled as noted in #6562. The fix committed in f844e96 increased the upper bound of random values before flooring to (array.length + 0.5), which addressed the issue of undersampling the last element but now also samples past the end. Furthermore, it intentionally continues to undersample the first element (despite what it says in the 3.70.0 release note).

The fix suggested by @samme in #6562 would have corrected undersampling on both ends but it was rejected. To produce a distribution almost identical to the one before v3.70.0 except without undersampling the last element, the function sould be:

weightedPick: function (array)
{
    return array[~~(Math.pow(this.frac(), 2) * (array.length - 0.5) + 0.5)];
},

The modified distribution is in green:
image

@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

@jameskirkwood
Copy link
Author

Thank you! The example project cited in the issue description now runs correctly using the dev build on labs.phaser.io:
https://labs.phaser.io/view.html?src=src\tilemap\endless%20map.js&v=dev

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

No branches or pull requests

3 participants