Download the PHP package frank-houweling/weighted-random without Composer
On this page you can find all versions of the php package frank-houweling/weighted-random. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download frank-houweling/weighted-random
More information about frank-houweling/weighted-random
Files in frank-houweling/weighted-random
Package weighted-random
Short Description Library for generating a weighted random sample
License MIT
Informations about the package weighted-random
Weighted random
This library is used to pick random values from a set of registered values, where values with a higher weight have a larger probability to be picked.
Installing
Install this library using composer.
Getting started
Using this library is simple. Initiate the WeightedRandomGenerator, register the possible values, and start generating random values!
Registering values
You can set one value with its weight using the following method. A higher weight means a higher probability that the value is generated by the random generator.
You can register multiple values at once with the registerValues() method. This method only accepts strings an integers as values.
The registerValues() methods expects the value to be the key, and the weight to be the value of the passed array.
You can remove a value from the generator with the removeValue method.
If you prefer, you can also use the WeightedValue valueobject. You should use the registerWeightedValue and removeWeightedValue methods to register and remove values with these value objects.
Retrieving registered values
You can retrieve registered values and their weights from the generator. These are always returns as WeightedValue valueobjects.
Generating a random sample
To generate a random sample from the registered values, we use the generate method.
Generated values are not always unique. It is possible that calling the generate method twice will return in the same value twice.
You can also generate a sample of multiple values using the generateMultiple method, and a set of multiple values without a possibility of duplicates with generateMultipleWithoutDuplicates.