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() looks biased #6562

Closed
samme opened this issue Jul 28, 2023 · 1 comment
Closed

weightedPick() looks biased #6562

samme opened this issue Jul 28, 2023 · 1 comment

Comments

@samme
Copy link
Contributor

samme commented Jul 28, 2023

Version

  • Phaser Version: 3.0.0 to 3.60.0

Description

shy and wayfinder reported/tested this on Discord.

It looks like weightedPick() undersamples the first and last array members.

Log-linear plot of weightedPick distribution

Example Test Code

https://codepen.io/samme/pen/qBQKYxN?editors=0110

Additional Information

I think it's supposed to be

return array[~~(Math.pow(this.frac(), 2) * array.length)];
@photonstorm
Copy link
Collaborator

Thanks. I think the distribution looks better with return array[~~(Math.pow(this.frac(), 2) * array.length + 0.5)];:

image

As without the 0.5, the first element is massively favoured:

image

But it definitely looks wrong with the array.length - 1.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants