2017 © Pedro Peláez
 

library php-biased-random

image

davidgorges/php-biased-random

  • Saturday, July 12, 2014
  • by dgorges
  • Repository
  • 1 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

php-biased-random

Get random elements based on probabilty, (*1)

How to Use

$randomizer = new Randomizer();
$randomizer->add( new Element('Banana', 70))
           ->add( new Element('Apple',  30));

$randomFruit = $randomizer->get(); // Chance of Banana is 70%

Calculate Probability

In some cases, you want to add multiple items and retrieve the probabilty of an item., (*2)

$randomizer = new Randomizer();
$randomizer->add( new Element('Banana', 2))
           ->add( new Element('Apple',  1))
           ->add( new Element('Banana', 2));

$probability = $randomizer->getProbabilityFor('Banana');
echo $probability; // outputs 0.8

Shorthand methods

You don't need to create Element wrapper objects. You can just add your elements to the Randomizer:, (*3)

$randomizer = new Randomizer();
$randomizer->add('banana')->add('apple');

// with weight
$randomizer->add('banana', 10)->add('apple', 5);

Note: If you add multiple identical items, they will be combined into a single element with adjusted weight., (*4)

The Versions

12/07 2014

dev-master

9999999-dev http://www.davidgorges.de/

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

statistics library random randomizer probability