-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandData.php
65 lines (51 loc) · 1.37 KB
/
RandData.php
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
57
58
59
60
61
62
63
64
65
<?php namespace Devtools;
class RandData
{
private $dataTypes;
public function get($type)
{
$func = 'rand'.ucfirst(strtolower($type));
if (in_array(ucfirst(strtolower($type)), $this->dataTypes)) {
return $this->$func();
} else {
throw new \InvalidArgumentException("Data of type $type could not be generated.");
}
}
public static function randArray($max = 100)
{
$array = array();
$arrayLen = rand()%$max;
for ($count = 0; $count<$arrayLen; $count++) {
array_push($array, randData::randSign()*rand());
}
return $array;
}
public static function randInteger($max = PHP_INT_MAX)
{
return randData::randSign()*rand()%$max;
}
public static function randDouble($max = 0)
{
if ($max == 0) {
$max = mt_getrandmax();
}
return randData::randSign()*mt_rand() / $max * mt_rand();
}
public static function randSign()
{
return pow(-1, rand(0, 1));
}
public static function randBool()
{
return (bool) rand(0, 1);
}
public static function randString($max = 100)
{
$stringLen = rand()%$max;
$string = "";
for ($i = 0; $i < $stringLen; $i++) {
$string .= chr(rand()%255);
}
return $string;
}
}