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.
Result of php executing
Full code of ar srand.php
- <?php
- class testSkillChange {
- public $skills;
- public $data;
- public $config;
- public $test;
- function __construct(){
- $this->skills = [ 'st', 'tk', 'ps', 'sh' ];
- $this->config = (object)[ 'skill_var'=>$this->skills ];
- }
- function reseed(){
- list($usec,$sec) = hrtime();
- $seed = ($sec + $usec) * 1000000;
- mt_srand($seed);
- }
- function runTest( $func, $times = 1000000 ){
- if($times<1) return;
- $this->reseed();
- $nl = "\n";
- $this->test = [];
- for( $x = 0; $x<$times; ++$x ){
- $skill = $this->$func();
- if(!isset($this->test[ $skill ])) $this->test[ $skill ] = (int)0;
- ++$this->test[$skill];
- }
- echo '/*** TEST '.$func.' STARTS ***/'.$nl;
- foreach ($this->skills as $sname){
- echo strtoupper($sname).' '.str_pad($this->data[$sname],3,' ',STR_PAD_LEFT).' ';
- if(!isset($this->test[$sname])){
- echo str_pad('0',3).'% (-)';
- } else {
- echo str_pad((($this->test[$sname]/$times)*100),3,' ',STR_PAD_LEFT).'% ('.$this->test[$sname].')';
- }
- echo $nl;
- }
- echo '/*** TEST '.$func.' ENDS ***/'.$nl;
- }
- function generate_skill_change2($way='+'){
- $weights = [];
- $total = 0;
- foreach ($this->config->skill_var as $skill) {
- $total += $this->data[$skill];
- }
- foreach ($this->config->skill_var as $skill) {
- if ($way=='-' and $this->data[$skill]<2) continue;
- $weights[ $skill ] = (int)(( $this->data[$skill] / $total ) * 100);
- }
- if (empty($weights)) {
- return '';
- }
- $skill = $this->random_weighted($weights);
- return $skill;
- }
- function generate_skill_change($way='+') {
- // svaru random ar vērtību kvadrātā
- $weighted = array();
- foreach ($this->config->skill_var as $skill) {
- if ($way=="-" and $this->data[$skill]<2) continue;
- $weighted[$skill]=$this->data[$skill]*$this->data[$skill];
- }
- if (empty($weighted)) {
- return '';
- }
- $skill = $this->random_weighted($weighted);
- return $skill;
- }
- function random_weighted(array $weightedValues) {
- $rand = mt_rand(1, (int) array_sum($weightedValues));
- foreach ($weightedValues as $key => $value) {
- $rand -= $value;
- if ($rand <= 0) {
- return $key;
- }
- }
- }
- }
- $test = new testSkillChange();
- //
- $test->data = [
- 'st' => 1,
- 'tk' => 1,
- 'ps' => 10,
- 'sh' => 1
- ];
- $test->runTest('generate_skill_change');
- $test->runTest('generate_skill_change2');
- $test->data = [
- 'st' => 1,
- 'tk' => 1,
- 'ps' => 1,
- 'sh' => 21
- ];
- $test->runTest('generate_skill_change');
- $test->runTest('generate_skill_change2');
- $test->data = [
- 'st' => 6,
- 'tk' => 15,
- 'ps' => 3,
- 'sh' => 10
- ];
- $test->runTest('generate_skill_change');
- $test->runTest('generate_skill_change2');
- $test->data = [
- 'st' => 1,
- 'tk' => 3,
- 'ps' => 18,
- 'sh' => 3
- ];
- $test->runTest('generate_skill_change');
- $test->runTest('generate_skill_change2');
- ?>
File Description
- ar srand
- PHP Code
- 26 Dec-2022
- 2.72 Kb
You can Share it:
Latest PHP Pastes