dimanche 1 mai 2016

Eloquent - biased random selection of a record

I have a model Quote, which has two properties: quote and probability. I want to select a random quote, but quotes with higher probability must be selected more often. For example, if we have

$q1->probability == 0.15

and

$q2->probability == 0.75

the latter must be 5 times more likely to be selected. The following command makes a random quote selection:

$quote = Quote::orderByRaw('RAND()')->first();

But I need the selection to be biased, as it was mentioned above. How to achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire