Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/AbService.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public function getVariant(string $experiment): string

public function getHash(string $uid, string $experiment): float
{
$hex = substr(hash('sha256', $uid . $experiment), 0, 16);
$hex = substr(hash('sha256', $uid . $experiment), 0, 15);
$int = hexdec($hex);

return $int / 0xFFFFFFFFFFFFFFFF;
return $int / 2 ** 60;
}

public function chooseVariant(float $hash, array $variants): string
Expand All @@ -122,8 +122,9 @@ public function chooseVariant(float $hash, array $variants): string

$names = array_keys($variants);
$weights = array_values($variants);
$nameCount = count($names);

for ($i = 0; $i < count($names) -1; $i++) {
for ($i = 0; $i < $nameCount - 1; $i++) {
$sum += $weights[$i];

if ($hash < $sum) {
Expand Down
2 changes: 1 addition & 1 deletion test/AbServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testGetVariant(): void
public function testGetVariantDistributed(): void
{
$this->markTestSkipped("takes a while");

$iterations = 1000000;
$delta = $iterations / 500; // 0.2% difference

Expand Down
Loading