Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //REPLACEMENTS
- function getList($A,$k) {
- $result = [];
- while (count($result) < $k) {
- $sum = 0;
- foreach ($A as $key => $value) {
- $sum += $value;
- }
- $rnd = rand(0,$sum * 100000) / 100000;
- $sum = 0;
- foreach ($A as $key => $value) {
- $sum += $value;
- if ($rnd <= $sum) {
- $result[] = $key;
- break;
- }
- }
- }
- return $result;
- }
- function getQi($A,$k) {
- foreach ($A as $key => $value) {
- $A[$key] = 1 - pow(1 - $value,1.0 / $k);
- }
- return $A;
- }
- //P_i percentages for list A. SUMS to 10 or in this case K
- $list = [
- "A" => .25,
- "B" => .25,
- "C" => .25,
- "D" => .25,
- "E" => .65,
- "F" => .65,
- "G" => .25,
- "H" => .25,
- "I" => .52,
- "J" => .5,
- "K" => .25,
- "L" => .25,
- "M" => .25,
- "N" => .25,
- "O" => .25,
- "P" => .25,
- "Q" => .28,
- "R" => .3,
- "S" => .25,
- "T" => .25,
- "U" => .5,
- "V" => .5,
- "W" => .5,
- "X" => .5,
- "Y" => .7,
- "Z" => .9
- ];
- //Occurrences list for keeping track
- $occurences = [
- "A" => 0,
- "B" => 0,
- "C" => 0,
- "D" => 0,
- "E" => 0,
- "F" => 0,
- "G" => 0,
- "H" => 0,
- "I" => 0,
- "J" => 0,
- "K" => 0,
- "L" => 0,
- "M" => 0,
- "N" => 0,
- "O" => 0,
- "P" => 0,
- "Q" => 0,
- "R" => 0,
- "S" => 0,
- "T" => 0,
- "U" => 0,
- "V" => 0,
- "W" => 0,
- "X" => 0,
- "Y" => 0,
- "Z" => 0
- ];
- //Algorithm
- //First get the Q_i percentages
- $probs = getQi($list,10);
- //Do it 100,000 times
- for ($i=0; $i < 100000; $i++) {
- //Generate the list using getList function Algorithm
- $result = getList($probs,10);
- //Go through each letter A to Z
- foreach ($occurences as $key => $value) {
- //If the Generated list $result contains the letter at least once, increment its occurrence variable
- if (in_array($key,$result))
- $occurences[$key] += 1;
- }
- }
- foreach ($occurences as $key => $value) {
- $occurences[$key] = $value / 1000;
- }
- printArray($occurences);
- //Result of printing is
- /*
- Array
- (
- [A] => 20.585
- [B] => 20.312
- [C] => 20.692
- [D] => 20.599
- [E] => 20.591
- [F] => 20.729
- [G] => 20.557
- [H] => 20.597
- [I] => 42.436
- [J] => 42.436
- [K] => 20.734
- [L] => 20.322
- [M] => 20.56
- [N] => 20.691
- [O] => 20.567
- [P] => 20.551
- [Q] => 23.119
- [R] => 24.879
- [S] => 20.81
- [T] => 20.605
- [U] => 42.478
- [V] => 42.379
- [W] => 42.774
- [X] => 42.765
- [Y] => 61.587
- [Z] => 83.658
- )
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement