2017 © Pedro Peláez
 

library shuffle-bag

Shuffle Bag for better random distribution

image

battlerattle/shuffle-bag

Shuffle Bag for better random distribution

  • Monday, July 29, 2013
  • by BattleRattle
  • Repository
  • 3 Watchers
  • 3 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Shuffle Bag

Build status: Build Status, (*1)

Scrutinizer: Scrutinizer Quality Score, (*2)

Packagist: Latest Stable Version, (*3)

This is a Shuffle Bag implementation, highly inspired by this nice article and its C# example., (*4)

What is a Shuffle Bag?

A Shuffle Bag can be used for picking random items while reaching the targeted distribution after max. X picks, where X means the sum of all amounts the items were added., (*5)

The Shuffle Bag could be used in games in order to provide a certain amount of randomness while having the guarantee to keep the game balanced at the same time., (*6)

Installation

The easiest way to install this library is to use Composer Add the package "battlerattle/shuffle-bag" to your composer.json, (*7)

{
    "require": {
        "battlerattle/shuffle-bag": "v1.0-beta"
    }
}

Example

Imagine you want 3 different items to be picked with a probability of 10%, 30% and 60%. Add those items 1, 3 and 6 times to your bag and after 10 picks you got the targeted distribution. Add those items 10, 30 and 60 times in order to get a better randomness, because you could get the first item 10 times in row by random, but after max. 100 picks, you reached the target distribution again., (*8)

Usage

Here we implement the example as described above:, (*9)

use BattleRattle\ShuffleBag\ArrayShuffleBag;

$bag = new ArrayShuffleBag();

// add your items with a certain probability
$bag->add('item 1', 1);
$bag->add('item 2', 3);
$bag->add('item 3', 6);

// pick a random item
$item = $bag->next();

In order to use the same bag over multiple requests, you need to store the bag in a persistent storage. Here we use the PersistentShuffleBag with Redis (Predis) as storage:, (*10)

use BattleRattle\ShuffleBag\PersistentShuffleBag;
use BattleRattle\ShuffleBag\Storage\RedisStorage;
use Predis\Client;

$redisClient = new Client();
$storage = new RedisStorage($redisClient);
$bag = new PersistentShuffleBag($storage);

$bag->add('foo', 42);
$bag->add('bar', 1337);

$item = $bag->next();

The Versions

29/07 2013

dev-master

9999999-dev

Shuffle Bag for better random distribution

  Sources   Download

MIT

The Development Requires

by Norman Soetbeer

20/04 2013

v1.0-beta

1.0.0.0-beta

Shuffle Bag for better random distribution

  Sources   Download

MIT

by Norman Soetbeer