-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[9.x] Introduce lottery helper class #42919
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
Conversation
|
Absolutely love this idea. I can think of several apps where seeding a set of data that needs to skew a certain way was very necessary and the pain points involved in getting that set up to work were pretty real! Think the API for this is spot on - hope it gets merged |
|
Love it! I wonder if $winner = Lottery::odds(1, 10)->check();
$sample = Lottery::odds(1, 100)->check(10); |
|
Also, could this PR be extended to use this implementation for the existing session lottery? |
|
Moving to draft, as I just realised I need to add some testing helpers so you can control the outcome in your tests! Will follow up tomorrow with them. Thanks for the feedback everyone. Keep it coming. Will respond tomorrow 💖 |
|
Fantastic!! 🎉 |
It absolutely could be, but I didn't want to make that change just for the sake of it. If Taylor is keen on making that change, I'd be happy to update the PR to migrate the existing lotteries (session and DB locks) over to this functionality.
I don't mind this either! I was thinking "choosing" a winner, but I also like "check". Would love thoughts on the naming for others either way! |
|
This is excellent. |
|
The requirement to pass in the numerator and denominator might be a little restrictive. I could think of situations where I have a float between 0 and 1 that is generated by something else and I'd like that to determine the lottery odds. If you accepted the odds in this form, anyone could do |
|
Firstly, let me state that I voted down because the contents of this class can too easily be distilled to: if(rand(0,99) >= 75) {
// win
} else {
// lose
}Therefore I consider it bloat. If however it would become a part of the framework, it would be nice if you could simply construct using |
|
I would agree with some of the other comments about allowing other ways of setting the odds, especially a float between 0 and 1. |
|
@bert-w could you write a phpunit test testing both the if and else condition on your snippet as easily and elegant as with the proposed Lottery class? |
|
Appreciate the ongoing feedback folks. Will push an update to support a single float value as probability early next week. |
You have a point there. There is an odd solution available though: // Set seed for random number generator:
srand(1234);
var_dump(rand(0,99));The result of |
|
Noice! How about |
|
Hey folks, I'm gonna temporarily close this one, but it will re-open it shortly. I'm gonna try and get a few additional features in place which will make this PR even more meaningful to the framework and community. Stay tuned... |
|
So I force pushed and I can't re-open this particular pull request, so I have created a new one: #44894 |
This PR introduces a new Lottery class. In isolation, this is not super useful, but when used with other features within Laravel and your own application, it can make for a nice experience.
Lotteries can be used in an assortment of ways, for example to do random sampling of an event or assign users to an A/B test group, etc.
It is also useful with built in framework features...
As the Lottery class is
callable, it may be passed directly in as a handler to some of Laravel's features. The following is an example of its use with the newDB::whenQueryingForLongerThanfunctionality.It could also be utilised with the existing n+1 reporting. This is especially useful because unlike the above function, the n+1 handler function may be called thousands of times per request...
You will notice in the above examples that we did not call
->choose(). This is because, an previously mentioned, the Lottery iscallable. Additionally you will notice that the closures passed to the lottery receive the arguments$modeland$relation.I wrote a blog post introducing this idea of randomly sampling lazy loading violations via lottery after we implemented this at my
$previousGigFull API examples
This new class could also be utilised in the framework where we perform lotteries, i.e. in the session and database lock garbage collection. I've not included that in this PR as I don't think we need to do it just for the sake of it. This is, after all, primarily a new user-facing feature.
I've also added some testing helpers similar to those found in other Laravel support classes.