Created
August 7, 2017 07:06
-
-
Save vuthaihoc/ce10a6404c04435c7ca54ae84bad3ea6 to your computer and use it in GitHub Desktop.
Simple random item generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: hocvt | |
* Date: 8/7/17 | |
* Time: 10:29 | |
*/ | |
namespace App\Colombo\Libs; | |
class SimpleRandom { | |
private $items = []; | |
private $total = 0; | |
/** | |
* SimpleRandom constructor. | |
* | |
* @param array $items \ | |
* @param int|string $mode 1|100|number | |
*/ | |
public function __construct(array $items = [], $mode = 'number') { | |
$function = 'importInMode' . ucfirst($mode); | |
$this->{$function}($items); | |
$this->shuffleItems(); | |
} | |
private function importInMode1($input = []){ | |
$result = []; | |
$total = 0; | |
foreach ($input as $k => $v){ | |
if (is_array($v)){ | |
$result[$v['key']] = ceil(floatval($v['value']) * 100); | |
}else{ | |
$result[$k] = ceil(floatval($v) * 100); | |
} | |
$total += $result[$k]; | |
} | |
if($total != 100){ | |
throw new \Exception("Tong gia tri khac 100%"); | |
} | |
$this->total = $total; | |
$this->items = $result; | |
} | |
private function importInMode100($input = []){ | |
$result = []; | |
$total = 0; | |
foreach ($input as $k => $v){ | |
if (is_array($v)){ | |
$result[$v['key']] = $v['value']; | |
}else{ | |
$result[$k] = $v; | |
} | |
$total += $result[$k]; | |
} | |
if($total != 100){ | |
throw new \Exception("Tong gia tri khac 100"); | |
} | |
$this->total = $total; | |
$this->items = $result; | |
} | |
private function importInModeNumber($input = []){ | |
$result = []; | |
$total = 0; | |
foreach ($input as $k => $v){ | |
if (is_array($v)){ | |
$result[$v['key']] = isset($v['value']) ? intval($v['value']) : 1; | |
}elseif(is_numeric($k)){ | |
$result[$v] = 1; | |
}else{ | |
$result[$k] = $v; | |
} | |
$total += $result[$k]; | |
} | |
$this->total = $total; | |
$this->items = $result; | |
} | |
private function shuffleItems(){ | |
} | |
private function getKey($randInt){ | |
if($randInt > $this->total){ | |
$randInt = $randInt % $this->total; | |
} | |
foreach ($this->items as $k => $v){ | |
if($randInt < $v){ | |
return $k; | |
}else{ | |
$randInt -= $v; | |
} | |
} | |
} | |
private function randomOne(){ | |
$randomInt = rand(0, $this->total - 1); | |
return $this->getKey($randomInt); | |
} | |
public function getRandom($number = null){ | |
if(!$number){ | |
return $this->randomOne(); | |
}else{ | |
$result = []; | |
for ($i = 0; $i<$number; $i++){ | |
$result[] = $this->randomOne(); | |
} | |
return $result; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use App\Colombo\Libs\SimpleRandom; | |
use Illuminate\Console\Command; | |
class TestSimpleRandom extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'simplerandom'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$times = 1000000; | |
$random_items = [ | |
'downloadable' => 0.2, | |
'google' => 0.4, | |
'dropbox' => 0.4 | |
]; | |
$generator = new SimpleRandom($random_items, 1); | |
$random_counter = [ | |
'downloadable' => 0, | |
'google' => 0, | |
'dropbox' => 0 | |
]; | |
$t_start = microtime(true); | |
for ($i = 0; $i < $times; $i++){ | |
$k = $generator->getRandom(); | |
$random_counter[$k]++; | |
} | |
$t_end = microtime(true); | |
$this->info("Run " . $times . " times in " . round($t_end - $t_start, 2) . "second(s)"); | |
dd($random_counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment