Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\Provider;
4
 
5
class Biased extends Base
6
{
7
    /**
8
     * Returns a biased integer between $min and $max (both inclusive).
9
     * The distribution depends on $function.
10
     *
11
     * The algorithm creates two doubles, x ∈ [0, 1], y ∈ [0, 1) and checks whether the
12
     * return value of $function for x is greater than or equal to y. If this is
13
     * the case the number is accepted and x is mapped to the appropriate integer
14
     * between $min and $max. Otherwise two new doubles are created until the pair
15
     * is accepted.
16
     *
17
     * @param int      $min      Minimum value of the generated integers.
18
     * @param int      $max      Maximum value of the generated integers.
19
     * @param callable $function A function mapping x ∈ [0, 1] onto a double ∈ [0, 1]
20
     *
21
     * @return int An integer between $min and $max.
22
     */
23
    public function biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt')
24
    {
25
        do {
26
            $x = mt_rand() / mt_getrandmax();
27
            $y = mt_rand() / (mt_getrandmax() + 1);
28
        } while (call_user_func($function, $x) < $y);
29
 
30
        return (int) floor($x * ($max - $min + 1) + $min);
31
    }
32
 
33
    /**
34
     * 'unbiased' creates an unbiased distribution by giving
35
     * each value the same value of one.
36
     *
37
     * @return int
38
     */
39
    protected static function unbiased()
40
    {
41
        return 1;
42
    }
43
 
44
    /**
45
     * 'linearLow' favors lower numbers. The probability decreases
46
     * in a linear fashion.
47
     *
48
     * @return int
49
     */
50
    protected static function linearLow($x)
51
    {
52
        return 1 - $x;
53
    }
54
 
55
    /**
56
     * 'linearHigh' favors higher numbers. The probability increases
57
     * in a linear fashion.
58
     *
59
     * @return int
60
     */
61
    protected static function linearHigh($x)
62
    {
63
        return $x;
64
    }
65
}