-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-deterministic-history.php
More file actions
69 lines (50 loc) · 1.93 KB
/
07-deterministic-history.php
File metadata and controls
69 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
require __DIR__.'/../../vendor/autoload.php';
use JOOservices\UserAgent\Service\UserAgentService;
use JOOservices\UserAgent\Spec\GenerationSpec;
use JOOservices\UserAgent\Spec\RandomSpec;
$service = new UserAgentService;
echo "=== Deterministic Generation & History ===\n\n";
// 1. Deterministic Generation (Seeding)
// By passing the same seed, you get exactly the same sequence of User-Agents.
// This is critical for reproducible tests.
echo "1. Seeding with 999:\n";
$seed = 999;
$ua1 = $service->generate(seed: $seed);
$ua2 = $service->generate(seed: $seed);
echo " UA 1: $ua1\n";
echo " UA 2: $ua2\n";
if ($ua1 === $ua2) {
echo " [SUCCESS] Both UAs are identical.\n\n";
} else {
echo " [FAILURE] UAs differ!\n\n";
}
// 2. RandomSpec Configuration
// You can control the random behavior via RandomSpec embedded in GenerationSpec.
echo "2. Using RandomSpec in GenerationSpec:\n";
$specWithSeed = GenerationSpec::create()
->randomSpec(new RandomSpec(
seed: 555,
historyWindow: 5, // Remember last 5 UAs
retryBudget: 10 // Try 10 times to find a fresh UA
))
->build();
$ua3 = $service->generate($specWithSeed);
$ua4 = $service->generate($specWithSeed); // Same spec = Same seed = Same result
echo " UA 3: $ua3\n";
echo " UA 4: $ua4\n";
echo " (Note: They are identical because the seed is baked into the spec)\n\n";
// 3. Avoiding Repetition
// The Service automatically maintains an LRU (Least Recently Used) history
// to avoid returning the same UA consecutively if possible, but ONLY if
// the seed changes or is not provided.
// If you use a fixed seed, you force the RNG to be in the same state, so you get the same result.
echo "3. Consecutive Random selection (No Seed):\n";
$ua5 = $service->generate();
$ua6 = $service->generate();
echo " UA 5: $ua5\n";
echo " UA 6: $ua6\n";
if ($ua5 !== $ua6) {
echo " [info] UAs are different (as expected).\n";
}