-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandom.class.php
More file actions
executable file
·51 lines (46 loc) · 1.35 KB
/
Random.class.php
File metadata and controls
executable file
·51 lines (46 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class Random
{
const LEVEL_LOW = 1;
const LEVEL_MIDDLE = 2;
const LEVEL_HIGH = 3;
/*
* generate a digit in range of [$min, $max]
* origin: stackoverflow
* notice: this method tends to generate unique numbers compared with rand()
*/
public static function randomInt($min, $max)
{
$range = intval($max) - intval($min);
if ($range < 1) return $min; // not so random...
$log = ceil(log($range, 2));
$bytes = (int)($log / 8) + 1; // length in bytes
$bits = (int)$log + 1; // length in bits
$filter = (int)(1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return $min + $rnd;
}
/*
* generate random string of length $length
* level: LOW - only numbers, MIDDLE - plus letters(upper and lower), HIGH - plus special chars
*/
public static function randomString($strlen, $level = self::LEVEL_MIDDLE)
{
$alphabet = '0123456789';
if ($level > self::LEVEL_LOW) {
$alphabet .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$alphabet .= 'abcdefghijklmnopqrstuvwxyz';
}
if ($level > self::LEVEL_MIDDLE)
$alphabet .= '+-*/?!%`~@#^&(){}';
$length = strlen($alphabet);
$token = '';
for ($i = 0; $i < $strlen; $i++) {
$token .= $alphabet[self::randomInt(0, $length - 1)];
}
return $token;
}
}