ar srand - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of ar srand.php

  1. <?php
  2.  
  3. class testSkillChange {
  4.        
  5.         public $skills;
  6.         public $data;
  7.         public $config;
  8.        
  9.         public $test;
  10.  
  11.         function __construct(){
  12.                 $this->skills = [ 'st', 'tk', 'ps', 'sh' ];
  13.                 $this->config = (object)[ 'skill_var'=>$this->skills ];
  14.         }
  15.  
  16.         function reseed(){
  17.                 list($usec,$sec) = hrtime();
  18.                 $seed = ($sec + $usec) * 1000000;
  19.                 mt_srand($seed);
  20.         }
  21.  
  22.         function runTest( $func, $times = 1000000 ){
  23.                 if($times<1) return;
  24.                 $this->reseed();
  25.                 $nl = "\n";
  26.                 $this->test = [];
  27.                 for( $x = 0; $x<$times; ++$x ){
  28.                         $skill = $this->$func();
  29.                         if(!isset($this->test[ $skill ])) $this->test[ $skill ] = (int)0;
  30.                         ++$this->test[$skill];
  31.                 }
  32.                 echo '/*** TEST '.$func.' STARTS ***/'.$nl;
  33.                 foreach ($this->skills as $sname){
  34.                         echo strtoupper($sname).'   '.str_pad($this->data[$sname],3,' ',STR_PAD_LEFT).'   ';
  35.                         if(!isset($this->test[$sname])){
  36.                                 echo str_pad('0',3).'% (-)';
  37.                         } else {
  38.                                 echo str_pad((($this->test[$sname]/$times)*100),3,' ',STR_PAD_LEFT).'% ('.$this->test[$sname].')';
  39.                         }
  40.                         echo $nl;
  41.                 }
  42.                 echo '/*** TEST '.$func.' ENDS ***/'.$nl;
  43.         }
  44.  
  45.         function generate_skill_change2($way='+'){
  46.                 $weights = [];
  47.                 $total = 0;
  48.                 foreach ($this->config->skill_var as $skill) {
  49.                         $total += $this->data[$skill];
  50.                 }
  51.  
  52.                 foreach ($this->config->skill_var as $skill) {
  53.                         if ($way=='-' and $this->data[$skill]<2) continue;
  54.                         $weights[ $skill ] = (int)(( $this->data[$skill] / $total ) * 100);
  55.                 }
  56.  
  57.                 if (empty($weights)) {
  58.                         return '';
  59.                 }
  60.                
  61.                 $skill = $this->random_weighted($weights);
  62.                
  63.                 return $skill;
  64.         }
  65.  
  66.         function generate_skill_change($way='+') {
  67.  
  68.                 // svaru random ar vērtību kvadrātā
  69.                 $weighted = array();
  70.                
  71.                 foreach ($this->config->skill_var as $skill) {
  72.                         if ($way=="-" and $this->data[$skill]<2) continue;
  73.                         $weighted[$skill]=$this->data[$skill]*$this->data[$skill];
  74.                 }
  75.                
  76.                 if (empty($weighted)) {
  77.                         return '';
  78.                 }
  79.  
  80.                 $skill = $this->random_weighted($weighted);
  81.  
  82.                 return $skill;
  83.         }
  84.  
  85.         function random_weighted(array $weightedValues) {
  86.                 $rand = mt_rand(1, (int) array_sum($weightedValues));
  87.                
  88.                 foreach ($weightedValues as $key => $value) {
  89.                         $rand -= $value;
  90.                         if ($rand <= 0) {
  91.                                 return $key;
  92.                         }
  93.                 }
  94.         }
  95. }
  96.  
  97. $test = new testSkillChange();
  98.  
  99. // 
  100. $test->data = [
  101.         'st' => 1,
  102.         'tk' => 1,
  103.         'ps' => 10,
  104.         'sh' => 1
  105. ];
  106.  
  107. $test->runTest('generate_skill_change');
  108. $test->runTest('generate_skill_change2');
  109.  
  110. $test->data = [
  111.         'st' => 1,
  112.         'tk' => 1,
  113.         'ps' => 1,
  114.         'sh' => 21
  115. ];
  116.  
  117. $test->runTest('generate_skill_change');
  118. $test->runTest('generate_skill_change2');
  119.  
  120. $test->data = [
  121.         'st' => 6,
  122.         'tk' => 15,
  123.         'ps' => 3,
  124.         'sh' => 10
  125. ];
  126.  
  127. $test->runTest('generate_skill_change');
  128. $test->runTest('generate_skill_change2');
  129.  
  130. $test->data = [
  131.         'st' => 1,
  132.         'tk' => 3,
  133.         'ps' => 18,
  134.         'sh' => 3
  135. ];
  136.  
  137. $test->runTest('generate_skill_change');
  138. $test->runTest('generate_skill_change2');
  139.  
  140. ?>
File Description
  • ar srand
  • PHP Code
  • 26 Dec-2022
  • 2.72 Kb
You can Share it: