數組應按其值排序從高到低。使用概率分佈對數組進行排序
<?php
$items = array(
1 => f(1),
2 => f(2),
3 => f(3),
4 => f(4),
5 => f(5),
);
?>
排序後,我看看哪個項目1,2,3,4,5是第一個。我一遍又一遍地嘗試。 之後
- 5應該是5倍以上,1
- 4應該是四倍以上1
- 3應該是第一項中的第一項中的第一項三次超過1
- 4應該是第一個項目兩次超過2
- ...
一個想法是
<?php
function f(key) {
return key/random();
}
?>
其中,爲1'000'000嘗試導致
key | times on top | ratio with key one | expected ratio
----+--------------+--------------------+---------------
5 | 374'365 | 6.75 | 5
4 | 267'863 | 4.83 | 4
3 | 185'707 | i am so lazy ... | 3
2 | 116'618 | | 2
1 | 55'447 | 1 | 1
看起來奇怪的我,但也許
- 有以f一個簡單的問題?
- 有更好的f?
我的實現:
<?php
abstract class Test {
private $result;
protected abstract function f($x);
protected function iteration() {
$values = array(
1 => $this->f(1),
2 => $this->f(2),
3 => $this->f(3),
4 => $this->f(4),
5 => $this->f(5),
);
arsort($values);
$top = key($values);
if (!isset($this->result[$top])) {
$this->result[$top] = 1;
} else {
$this->result[$top]++;
}
}
public function run($iterations) {
$this->result = array();
for($i = 0; $i < $iterations; $i++) {
$this->iteration();
}
arsort($this->result);
return $this->result;
}
}
class MyTest extends Test {
protected function f($x) {
return $x/rand();
}
}
$test = new MyTest();
$result = $test->run(1000 * 1000);
print_r($result);
printf("Ratio of key 5 to 1, which should be 5: %f\n", $result[5]/$result[1]);
?>
我已經嘗試了十億輪。但是再比例是6.75 - 整點是:爲什麼不是五呢?
爲
<?php
class BetterRandomGeneratorTest extends Test {
protected function f($x) {
return $x/mt_rand();
}
}
?>
的結果是
Array
(
[5] => 3742816
[4] => 2674352
[3] => 1861444
[2] => 1168333
[1] => 553055
)
Ratio of key 5 to 1: 6.767529
強大數定律說這是真的。做十億次嘗試。 – Blender 2011-04-27 23:18:28
你在找什麼?我不明白你是否試圖理解爲什麼/如何,或者如果你需要幫助實施你正在嘗試做的事情......試着讓你的問題更清楚一些。 – 2011-04-27 23:22:49
我同意。什麼是f(x)'應該完成? – Blender 2011-04-27 23:25:16