Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
28
Task/Knuths-algorithm-S/PHP/knuths-algorithm-s.php
Normal file
28
Task/Knuths-algorithm-S/PHP/knuths-algorithm-s.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
function s_of_n_creator($n) {
|
||||
$sample = array();
|
||||
$i = 0;
|
||||
return function($item) use (&$sample, &$i, $n) {
|
||||
$i++;
|
||||
if ($i <= $n) {
|
||||
// Keep first n items
|
||||
$sample[] = $item;
|
||||
} else if (rand(0, $i-1) < $n) {
|
||||
// Keep item
|
||||
$sample[rand(0, $n-1)] = $item;
|
||||
}
|
||||
return $sample;
|
||||
};
|
||||
}
|
||||
|
||||
$items = range(0, 9);
|
||||
|
||||
for ($trial = 0; $trial < 100000; $trial++) {
|
||||
$s_of_n = s_of_n_creator(3);
|
||||
foreach ($items as $item)
|
||||
$sample = $s_of_n($item);
|
||||
foreach ($sample as $s)
|
||||
$bin[$s]++;
|
||||
}
|
||||
print_r($bin);
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue