You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
/* * Generates a weighted random number * * Choose a continuous random number between 0..1 * Raise to a power γ, to bias it. 1 is unweighted, lower gives more of the higher numbers and vice versa * Scale to desired range and round to integer * * @author bobince <http://stackoverflow.com/a/445363/1112384> */publicstaticfunctionweightedRand($min, $max, $gamma) {
$offset = $max - $min + 1;
returnfloor($min + pow(lcg_value(), $gamma) * $offset);
}
Here is the probability distribution of calling weightedRand(1, 4, $gamma);.
May need some tweeking with values lower or equal to 0 though.
I would love this, when seeding fake data like friends on a social website, a lot of the time fake users have more fake friends than my testing account (which is sad I know).
@fzaninotto I don't know when you want to release 1.4, but if I have enough time I might get this done for 1.4 as well. It looks like Rejection sampling does work very well for these kind of things.
What about adding an generator for “weighted random numbers”?
For example a generator like this gamma “corrected” random number generator:
Here is the probability distribution of calling
weightedRand(1, 4, $gamma);
.May need some tweeking with values lower or equal to 0 though.
This might come in handy some times ^^
The text was updated successfully, but these errors were encountered: