Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1.  
  2. if(!function_exists("__")){
  3.     function __($msg){
  4.         $args = func_get_args();
  5.         #print_r(debug_backtrace());
  6.        if(count($args) > 1) $args = array_slice($args, 1);
  7.         else $args = array();
  8.         return vsprintf($msg, $args);
  9.     }
  10. }
  11.  
  12.  
  13. global $W;
  14. $W = array(0.25, 0.125, 0.05, 0.01, 0.00);
  15.  
  16.  
  17. $CHECKS = array(500, 1000, 2500, 5000, 10000);
  18.  
  19. $PROB = 0.95;
  20.  
  21. echo __("\n[#] PROB=%d%%\n\n", $PROB*100);
  22.  
  23. print_tr("-");
  24. print_row(array_merge(array(''), array_map(function($x){return $x > 0.0 ? ($x*100*2).'%' : 'max'; }, $W)));
  25. print_tr();
  26.  
  27. foreach($CHECKS as $number_of_checks){
  28.     $v = massive_check($PROB, $number_of_checks);
  29.     $v = array_map(function($x){ return __("%.2f%%", $x);}, $v);
  30.     array_unshift($v, str_pad(" ".$number_of_checks.":", 10));
  31.     print_row($v);
  32.     print_tr(($number_of_checks == $CHECKS[count($CHECKS)-1]) ? "-" : "+");
  33. }
  34.  
  35. function print_row($v, $row_splitter="|"){
  36.     echo '|'.implode($row_splitter, array_map(function($x){ return str_pad($x, 10, " ", STR_PAD_BOTH);}, $v)).'|' . "\n";
  37. }
  38. function print_tr($row_splitter="+"){
  39.     global $W;
  40.     #echo str_repeat("-", (count($W)+1)*10 + count($W) + 2)."\n";
  41.    $r = array();
  42.     for($i=1; $i<=count($W)+1; $i++){
  43.         $r[] = str_repeat("-", 10);
  44.     }
  45.     print_row($r, $row_splitter);
  46. }
  47.  
  48.  
  49. function massive_check($prob, $number_of_checks){
  50.     global $W;
  51.     $diff = array();
  52.     for($i=1; $i<=2000; $i++){
  53.         $v = massive_say($prob, ($cnt_max=$number_of_checks));
  54.         $diff[] = ($v-$prob)*100;
  55.     }
  56.     sort($diff);
  57.     $values=array();
  58.     foreach($W as $w){
  59.         $values[] = (abs($diff[intval(count($diff)*$w)]) + abs($diff[intval(count($diff)*(1-$w))-1]));
  60.     }
  61.  
  62.     return $values;
  63. }
  64.  
  65. function massive_say($prob, $cnt_max=1000, $cnt_max2=100000){
  66.     $yes = 0;
  67.     for($i=0; $i<$cnt_max; $i++){
  68.         $v=say_yes_or_no($prob, $cnt_max2);
  69.         $yes += $v >= 1 ? 1 : 0;
  70.     }
  71.     return ($yes/$cnt_max);
  72. }
  73.  
  74. function say_yes_or_no($prob, $max=100000){
  75.     #mt_srand();
  76.    return mt_rand(0, $max-1) < $max*$prob ? 1 : 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement