-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add RandomNumberGenerator::rand_weighted
method
#88883
Add RandomNumberGenerator::rand_weighted
method
#88883
Conversation
fc45dae
to
fd10665
Compare
You used a merge commit to update your branch, please use rebase in the future instead, see here You also need to use force pushing to update your branch, with |
9dce2f1
to
5b73724
Compare
Worth considering whether or not the Second, perhaps the method should be called And finally, the fact that there's no equivalent in the RandomNumberGenerator class means more "experienced" users can't use it, for predictable RNG with known seeds. |
c391b40
to
494a381
Compare
@Mickeon thanks for the review! Worth considering whether or not the weights parameter could be a PackedFloat64Array or even PackedFloat32Array. If it needs to be a number I don't see why not to, at a glance. GDScript would also do the conversion from an Array implicitly if it can. I can definitely make this change. Do you think it would perform better or not really? Second, perhaps the method should be called pick_weighted_randomto match pick_random. Makes sense. It would make it more straight forward to the developer I guess. And finally, the fact that there's no equivalent in the RandomNumberGenerator class means more "experienced" users can't use it, for predictable RNG with known seeds. Not really sure what you mean here. Can you clarify what would be the course of action here? |
The performance benefit would come from not having to check for the type of every element being a float, which is quite inefficient and rather silly because PackedFloat32Array would basically guarantee it.
Okay, so. RandomNumberGenerator is a class that can be used to hold self-sustaining random number generators that do not influence one another. |
494a381
to
2d06f61
Compare
3ced89b
to
4719652
Compare
4719652
to
4b7ee6e
Compare
RandomNumberGenerator::rand_weighted
method
ae7346c
to
b9fee07
Compare
b9fee07
to
1426028
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to add some tests, but matches the code referenced in godotengine/godot-proposals#3948 (comment)
Looks good and makes sense, some further feedback from the core team would be good though
The opportunity to have this in core excites me because there may actually be a faster algorithm out there but its obscurity would mean a "common" developer using the engine may not implement it this way and benefit from it. I've known about it for a while. If/when this PR is merged I'd love to take a stab at trying to implement it. |
The specific implementation is just that, an implementation detail, so we're free to improve it in the future :) |
Absolutely. We can always iterate on it and improve it at any time. |
c7e4588
to
3b6c196
Compare
3b6c196
to
3026d12
Compare
Could you squash the commits? See PR workflow for instructions. And while doing it, please edit the commit message to match the title of this PR / the new implementation. |
3026d12
to
88df5ea
Compare
all done! @akien-mga |
I was wondering if I should also add it to the C# API and added it to the Math namespace with the other random methods. @akien-mga @Mickeon @AThousandShips |
It's no longer a requirement since it belongs to RandomNumberGenerator now, and it's how this method can be accessed in C#. I think the PR is completely fine as is. |
Thanks! And congrats for your first merged Godot contribution 🎉 |
Thanks for the help all!! Really happy to have contributed! |
Hey, I'm looking through the code as I wanted to propose a change/addition related to this PR and the
Shouldn't it be |
Hey everyone, |
Because (if I recall correctly) we agreed that when working with weighted randomness it would be much more beneficial to have more granular control over it, and RandomNumberGenerator allows this. Although, I don't believe there's much stopping this from being in the Global Scope, as well. |
yeah. what @Mickeon said. |
This is the implementation for Add a method to get a random element with weighted probability (similar to random.choices() in Python) as in this Proposal.
Usage
It was added to the core classes as part of the RandomNumberGenerator class, where you pass a PackedFloat32Array to it as the weights.
I also added a test to check if size of both arrays match.
random.choices()
in Python) godot-proposals#3948