本文整理汇总了PHP中utility::random方法的典型用法代码示例。如果您正苦于以下问题:PHP utility::random方法的具体用法?PHP utility::random怎么用?PHP utility::random使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utility
的用法示例。
在下文中一共展示了utility::random方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: substr
static function generate_random_number($min = 0, $max = 0)
{
// generate seed. TO-DO: Look for a better seed value everytime
$seed = mt_rand();
// generate $random
// special thing about random is that it is 32(md5) + 40(sha1) + 40(sha1) = 112 long
// hence if we cut the 1st 8 characters everytime, we can get upto 14 random numbers
// each time the length of $random decreases and when it is less than 8, new 112 long $random is generated
if (strlen(self::$random) < 8) {
self::$random = md5(uniqid(microtime() . mt_rand(), true) . $seed);
self::$random .= sha1(self::$random);
self::$random .= sha1(self::$random . $seed);
}
// take first 8 characters
$value = substr(self::$random, 0, 8);
// strip first 8 character, leaving remainder for next call
self::$random = substr(self::$random, 8);
$value = abs(hexdec($value));
// Reduce the value to be within the min - max range. 4294967295 = 0xffffffff = max random number
if ($max != 0) {
$value = $min + ($max - $min + 1) * ($value / (4294967295 + 1));
}
return abs(intval($value));
}