-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.php
More file actions
56 lines (56 loc) · 1.25 KB
/
Random.php
File metadata and controls
56 lines (56 loc) · 1.25 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
52
53
54
55
56
<?php
class Random
{
public function __constructor()
{
//
}
public function random()
{
return mt_rand() / mt_getrandmax();
}
public function range(float $min = 0, float $max = 1)
{
return $min + ($this->random() * ($max - $min));
}
public function irange(int $min = 0, int $max = 1)
{
return (int)($this->range($min, $max));
}
public function choice($array)
{
return $array[$this->irange(0, count($array))];
}
public function choices($array, int $length)
{
$rarray = [];
for ($i=0; $i < $length; $i++)
{
$rarray[$i] = $this->choice($array);
}
return $rarray;
}
public function strrange(int $length, string $addchars = "")
{
$rstr = "";
$rastr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".$addchars;
$_ = strlen($rastr);
for ($i=0; $i < $length; $i++)
{
$rstr .= $rastr[$this->irange(0, $_)];
}
return $rstr;
}
public function chance(float $chance = 50.0)
{
if ($this->range(0, 100) <= $chance)
{
return true;
}
else
{
return false;
}
}
}
?>