Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Mash | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| mash | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| version | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Alea; |
| 5 | |
| 6 | /** |
| 7 | * This is a helper class which just takes a bunch of mixed seed content |
| 8 | * and smushes it together to create a single numeric seed. |
| 9 | */ |
| 10 | class Mash { |
| 11 | |
| 12 | /** @var int */ |
| 13 | private $n; |
| 14 | |
| 15 | public function __construct() { |
| 16 | $this->n = 0xefc8249d; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Mash in some more data. |
| 21 | * @param mixed $data Anything that can be an argument to `strval`. |
| 22 | * @return float The current mash |
| 23 | */ |
| 24 | public function mash( $data ): float { |
| 25 | $data = strval( $data ); |
| 26 | $data = mb_convert_encoding( $data, 'ucs-2' ); |
| 27 | $n = $this->n; |
| 28 | for ( $i = 0; $i < strlen( $data ); $i += 2 ) { |
| 29 | $charCode = ord( $data[$i] ) * 256 + ord( $data[$i + 1] ); |
| 30 | $n += $charCode; |
| 31 | $h = 0.02519603282416938 * $n; |
| 32 | $n = intval( $h ); |
| 33 | $h -= $n; |
| 34 | $h *= $n; |
| 35 | $n = intval( $h ); |
| 36 | $h -= $n; |
| 37 | $n += $h * 0x100000000; // 2^32 |
| 38 | } |
| 39 | $this->n = $n; |
| 40 | return intval( $n ) * 2.3283064365386963e-10; // 2^-32 |
| 41 | } |
| 42 | |
| 43 | public static function version(): string { |
| 44 | return 'Mash 0.9'; |
| 45 | } |
| 46 | } |