Random Code(Color Hex Generator)

Random Code(Color Hex Generator)
Gavin Wednesday 21st June 2023, 17:58:14I don't know why I am doing this, but I have made a custom HEX color generator
<?php
//$loop is how many generated hex code you would want to have.
function genHex(int $loop):array {
$hex = array();
$list = '0123456789abcdef';
for($i=0;$i<$loop;$i++){
$h='#';
for($k=0;$k<6;$k++){
$rand = rand(0,strlen($list)-1);
$l = str_split($list);
$h.=$l[$rand];
}
$hex[$h] = $h;
}
return $hex;
}
?>
Last modified by Gavin on Thursday 22nd June 2023, 11:50:00
A Web developer and web security programmer
Replies 3

That's great, but you can do it more simply like this π :
function genHex(int $loop): array {
$hexArray = [];
for ($i = 0; $i < $loop; $i++) {
$hexArray[] = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
return $hexArray;
}
Last modified by Simon on Sunday 25th June 2023, 17:28:00

I was always told that simple is lazy work. π
I don't know why, I take the longest and more complex why, then I should have attended,
Thanks for the "shorted version" of it.
I don't know why, I take the longest and more complex why, then I should have attended,
"if it works, then it's fine"
Thanks for the "shorted version" of it.
Last modified by Gavin on Sunday 25th June 2023, 18:49:00
A Web developer and web security programmer
Suggested Topics

AlexanderLiebrecht@5e3cf8af
started π©πͺ All Users cozy Christmas and Thank you for v1.0.4
Open Bar

Fred
started FlatBoard v0.8 Beta
Announcements

Alexander
started solved Error 500 on the index page after Update to Flatboard 2.0
Bug reports

T@bef7e829
started Large Flatboard forums?
Open Bar