HashRand   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A randomInt() 0 8 1
A init() 0 4 1
1
<?php
2
3
namespace Savvot\Random;
4
5
/**
6
 * Proof-of-concept that md5 hash is uniformly distributed and can be used as PRNG source
7
 * Pretty fast, simple and straightforward generator
8
 *
9
 * @package Savvot\Random
10
 * @author  SavvoT <[email protected]>
11
 */
12
class HashRand extends AbstractRand
13
{
14
    /**
15
     * This is 63bit generator because PHP does not support unsigned 64bit int
16
     */
17
    const INT_MAX = 0x7FFFFFFFFFFFFFFF;
18
19
    /**
20
     * @inheritdoc
21
     */
22
    public function randomInt()
23
    {
24
        $hash = md5($this->hashedSeed . $this->state++, true);
25
        $num = unpack('V*', $hash);
26
27
        // Create two 64bit numbers from four 32bit int and xor them
28
        return (($num[2] << 32 | $num[1]) ^ ($num[4] << 32 | $num[3])) & self::INT_MAX;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    protected function init()
35
    {
36
        $this->state = 0;
37
    }
38
}
39