Last active
November 30, 2022 14:17
-
-
Save avtobys/d0378bd3cd4eee8d3ba9f4868e149f81 to your computer and use it in GitHub Desktop.
class Random
This file contains 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 | |
class Random | |
{ | |
public static function gen($min, $max, $seed = '') | |
{ | |
$seed = self::seed($seed); | |
$sumbols = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$md5str = md5($seed); | |
$bigseed = ''; | |
foreach (str_split($md5str) as $item) { | |
$bigseed .= strpos($sumbols, $item); | |
} | |
$bigseed = substr($bigseed, 10, $max); | |
while ($bigseed > 2147483647) { | |
$bigseed = $bigseed / 10; | |
} | |
$seed = intval($bigseed); | |
mt_srand($seed); | |
return mt_rand($min, $max); | |
} | |
public static function gen2($string_or_array, $seed = '', $return_array = false, $uniq = true, $delimiter = ',') | |
{ | |
$seed = self::seed($seed); | |
$arr = is_array($string_or_array) ? $string_or_array : explode($delimiter, $string_or_array); | |
if (!is_array($string_or_array)) { | |
$arr = array_map('trim', $arr); | |
} | |
if ($uniq) { | |
$arr = array_unique($arr); | |
} | |
$uniq_keys = []; | |
foreach ($arr as $key => $item) { | |
$uniq_key = self::gen(0, 2147483647, $key . $seed); | |
while (in_array($uniq_key, $uniq_keys)) { | |
$uniq_key++; | |
} | |
$uniq_keys[] = $uniq_key; | |
} | |
$arr = array_combine($uniq_keys, $arr); | |
ksort($arr); | |
$arr = array_values($arr); | |
return $return_array ? $arr : $arr[0]; | |
} | |
private static function seed($seed) | |
{ | |
if (!$seed && !empty($_SERVER['REQUEST_URI'])) { | |
$seed = strtok($_SERVER['REQUEST_URI'], '?'); | |
} | |
if (!$seed) { | |
$seed = __FILE__; | |
} | |
return $seed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment