Biased::biasedNumberBetween()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 3
crap 3.0261
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DummyGenerator\Core;
6
7
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface;
8
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait;
9
use DummyGenerator\Definitions\Extension\BiasedExtensionInterface;
10
use DummyGenerator\Definitions\Extension\Exception\ExtensionArgumentException;
11
12
class Biased implements BiasedExtensionInterface, RandomizerAwareExtensionInterface
13
{
14
    use RandomizerAwareExtensionTrait;
15
16 4
    public function biasedNumberBetween(int $min = 0, int $max = 100, callable|string $function = 'sqrt'): int
17
    {
18 4
        if (!is_callable($function)) {
19
            throw new ExtensionArgumentException('Given $function must be a callable');
20
        }
21
22
        do {
23 4
            $x = $this->randomizer->getInt(0, PHP_INT_MAX) / PHP_INT_MAX;
24 4
            $y = $this->randomizer->getInt(0, PHP_INT_MAX) / (PHP_INT_MAX + 1);
25 4
        } while ($function($x) < $y);
26
27 4
        return (int) floor($x * ($max - $min + 1) + $min);
28
    }
29
30 1
    public function unbiased(): int
31
    {
32 1
        return 1;
33
    }
34
35 1
    public function linearLow(float $x): float
36
    {
37 1
        return 1 - $x;
38
    }
39
40 1
    public function linearHigh(float $x): float
41
    {
42 1
        return $x;
43
    }
44
}
45