PHP code example of ipapikas / dice

1. Go to this page and download the library: Download ipapikas/dice library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ipapikas / dice example snippets


use \Dice\Distributions\CoinDistribution;
use \Dice\Random\MTRand;
use \Dice\Roller;

// Create new Distribution
$coin = new CoinDistribution();

// Create a new Randomizer
$randomizer = new MTRand();

// Create new Roller
$roller = new Roller($coin, $randomizer);

// "Roll" the Dice and get the next item from the distribution
// In this example, the value can be either 1 (Head) or 2 (Tail),
// based on the Distribution
$value = $roller->roll();

use Dice\Distributions\AbstractDistribution;
use Dice\Validators\ProbabilityValidator;
use Dice\Validators\UnBiasedValidator;

/**
 * Class WeatherDistribution
 */
class WeatherDistribution extends AbstractDistribution
{
    const SUNNY = 1;
    const RAIN = 2;
    const CLOUDY = 3;
    const SNOW = 4;

    /**
     * WeatherDistribution constructor.
     */
    public function __construct()
    {
        // Set validators
        $this->setValidators([
            new ProbabilityValidator(),
            new UnBiasedValidator(),
        ]);
    }

    /**
     * @return mixed
     */
    public function getItems()
    {
        return [
            self::SUNNY => 1 / 4,
            self::RAIN => 1 / 4,
            self::CLOUDY => 1 / 4,
            self::SNOW => 1 / 4,
        ];
    }
}

use Dice\Distributions\AbstractDistribution;
use Dice\Validators\ProbabilityValidator;

/**
 * Class RainyWeatherDistribution
 */
class RainyWeatherDistribution extends AbstractDistribution
{
    const SUNNY = 1;
    const RAIN = 2;
    const CLOUDY = 3;
    const SNOW = 4;

    /**
     * RainyWeatherDistribution constructor.
     */
    public function __construct()
    {
        // Set validators
        $this->setValidators([
            new ProbabilityValidator(),
        ]);
    }

    /**
     * @return mixed
     */
    public function getItems()
    {
        return [
            self::SUNNY => 1 / 9,
            self::RAIN => 2 / 3,
            self::CLOUDY => 1 / 9,
            self::SNOW => 1 / 9,
        ];
    }
}