66
77trait Randoms
88{
9- public static function randomBool (): bool
9+ /**
10+ * Generates a random boolean.
11+ *
12+ * @return bool
13+ */
14+ public static function randomBoolean (): bool
1015 {
11- return (bool ) rand (0 , 1 );
16+ return (bool )rand (0 , 1 );
1217 }
18+
19+ public static function randomColor (): string
20+ {
21+ $ CSS_COLOR_NAMES = [
22+ 'AliceBlue ' , 'AntiqueWhite ' , 'Aqua ' , 'Aquamarine ' , 'Azure ' ,
23+ 'Beige ' , 'Bisque ' , 'Black ' , 'BlanchedAlmond ' , 'Blue ' ,
24+ 'BlueViolet ' , 'Brown ' , 'BurlyWood ' , 'CadetBlue ' , 'Chartreuse ' ,
25+ 'Chocolate ' , 'Coral ' , 'CornflowerBlue ' , 'Cornsilk ' , 'Crimson ' ,
26+ 'Cyan ' , 'DarkBlue ' , 'DarkCyan ' , 'DarkGoldenRod ' , 'DarkGray ' ,
27+ 'DarkGrey ' , 'DarkGreen ' , 'DarkKhaki ' , 'DarkMagenta ' , 'DarkOliveGreen ' ,
28+ 'DarkOrange ' , 'DarkOrchid ' , 'DarkRed ' , 'DarkSalmon ' , 'DarkSeaGreen ' ,
29+ 'DarkSlateBlue ' , 'DarkSlateGray ' , 'DarkSlateGrey ' , 'DarkTurquoise ' ,
30+ 'DarkViolet ' , 'DeepPink ' , 'DeepSkyBlue ' , 'DimGray ' , 'DimGrey ' ,
31+ 'DodgerBlue ' , 'FireBrick ' , 'FloralWhite ' , 'ForestGreen ' , 'Fuchsia ' ,
32+ 'Gainsboro ' , 'GhostWhite ' , 'Gold ' , 'GoldenRod ' , 'Gray ' ,
33+ 'Grey ' , 'Green ' , 'GreenYellow ' , 'HoneyDew ' , 'HotPink ' ,
34+ 'IndianRed ' , 'Indigo ' , 'Ivory ' , 'Khaki ' , 'Lavender ' ,
35+ 'LavenderBlush ' , 'LawnGreen ' , 'LemonChiffon ' , 'LightBlue ' ,
36+ 'LightCoral ' , 'LightCyan ' , 'LightGoldenRodYellow ' , 'LightGray ' ,
37+ 'LightGrey ' , 'LightGreen ' , 'LightPink ' , 'LightSalmon ' ,
38+ 'LightSeaGreen ' , 'LightSkyBlue ' , 'LightSlateGray ' , 'LightSlateGrey ' ,
39+ 'LightSteelBlue ' , 'LightYellow ' , 'Lime ' , 'LimeGreen ' , 'Linen ' ,
40+ 'Magenta ' , 'Maroon ' , 'MediumAquaMarine ' , 'MediumBlue ' ,
41+ 'MediumOrchid ' , 'MediumPurple ' , 'MediumSeaGreen ' , 'MediumSlateBlue ' ,
42+ 'MediumSpringGreen ' , 'MediumTurquoise ' , 'MediumVioletRed ' ,
43+ ];
44+ return $ CSS_COLOR_NAMES [array_rand ($ CSS_COLOR_NAMES )];
45+ }
46+
47+ /**
48+ * Generates a random color in HEX format.
49+ *
50+ * @return string
51+ */
52+ public static function randomHexColor (): string
53+ {
54+ return sprintf ('#%06X ' , mt_rand (0 , 0xFFFFFF ));
55+ }
56+
57+ /**
58+ * Generates a random HSL color.
59+ *
60+ * @return string
61+ */
62+ public static function randomHsl (): string
63+ {
64+ $ h = rand (0 , 360 );
65+ $ s = rand (0 , 100 );
66+ $ l = rand (0 , 100 );
67+ return "hsl( $ h, $ s%, $ l%) " ;
68+ }
69+
70+ /**
71+ * Generates a random integer.
72+ *
73+ * @param int $min
74+ * @param int $max
75+ * @return int
76+ */
77+ public static function randomNumber ($ min = 0 , $ max = 100 ): int
78+ {
79+ return rand ($ min , $ max );
80+ }
81+
82+ /**
83+ * Generates a random string.
84+ *
85+ * @param int $length
86+ * @return string
87+ */
88+ public static function randomString ($ length = 8 ): string
89+ {
90+ $ chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ' ;
91+ $ count = mb_strlen ($ chars );
92+
93+ for ($ i = 0 , $ result = '' ; $ i < $ length ; $ i ++) {
94+ $ index = rand (0 , $ count - 1 );
95+ $ result .= mb_substr ($ chars , $ index , 1 );
96+ }
97+
98+ return $ result ;
99+ }
100+
13101}
0 commit comments