-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoller.php
147 lines (127 loc) · 3.65 KB
/
Roller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* This file is part of the Dice Package.
*
* (c) Ioannis Papikas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Dice;
use Dice\Distributions\Distribution;
use Dice\Exceptions\DistributionException;
use Dice\Helpers\NumberHelper;
use Dice\Random\Randomizer;
use Dice\Validators\Validator;
use InvalidArgumentException;
/**
* Class Roller
* @package Dice
*/
class Roller implements Validator
{
/**
* @var Distribution
*/
protected $distribution;
/**
* @var Randomizer
*/
protected $randomizer;
/**
* Roller constructor.
*
* @param Distribution $distribution
* @param Randomizer $randomizer
*/
public function __construct(Distribution $distribution, Randomizer $randomizer)
{
$this->distribution = $distribution;
$this->randomizer = $randomizer;
}
/**
* @param float|null $seed
* @param mixed $default
*
* @return int|string|null|mixed
* @throws DistributionException
*/
public function roll($seed = null, $default = null)
{
// Validate distribution
if (!$this->validate()) {
throw new DistributionException(__METHOD__ . ': Distribution is not valid.');
}
// Scale probabilities to have a better precision and get random
$maxRange = $this->getMaxRange();
$random = $this->getRandomizer()->next(1, $maxRange, $seed);
// Check which item was met
$base = 1;
foreach ($this->distribution->getItems() as $value => $probability) {
$upperBound = $base + (int)NumberHelper::floor($probability * $maxRange);
if ($random >= $base && $random <= $upperBound) {
return $value;
}
$base = $upperBound + 1;
}
// If no value is met, return default value
return $default;
}
/**
* @return bool
*/
public function validate()
{
return $this->distribution->validate();
}
/**
* @return int
*/
public function getMaxRange()
{
// Initialize number of decimals
$numbersOfDecimals = 1;
// Get all cases with their probabilities
$probabilities = $this->getDistribution()->getItems();
foreach ($probabilities as $probability) {
try {
$probabilityNumberOfDecimals = NumberHelper::numberOfDecimals($probability);
} catch (InvalidArgumentException $ex) {
$probabilityNumberOfDecimals = 1;
}
$numbersOfDecimals = $probabilityNumberOfDecimals > $numbersOfDecimals ? $probabilityNumberOfDecimals : $numbersOfDecimals;
}
// Convert number of decimals to power of 10 to get maximum number
$maxRange = pow(10, $numbersOfDecimals);
// Check given max range against randomizer max range
return $maxRange > $this->getRandomizer()->max() ? $this->getRandomizer()->max() : $maxRange;
}
/**
* @return Distribution
*/
public function getDistribution()
{
return $this->distribution;
}
/**
* @param Distribution $distribution
*/
public function setDistribution(Distribution $distribution)
{
$this->distribution = $distribution;
}
/**
* @return Randomizer
*/
public function getRandomizer()
{
return $this->randomizer;
}
/**
* @param Randomizer $randomizer
*/
public function setRandomizer(Randomizer $randomizer)
{
$this->randomizer = $randomizer;
}
}