I'm new to PHP & Stackoverflow. Correct my questions if I make a mistake here. My goal to accomplish here is to get a random color (for loop) followed by the sequence (foreach) accordingly. Here is my code below.
$color = array("green", "red", "yellow");
$sequence = array("R", "B", "B");
for ($x = 1; $x <= 10; $x ) {
$color_random = $color[array_rand($color)];
echo $color_random . "- ";
}
foreach ($sequence as $sequence_value) {
echo $sequence_value . "- ";
}
Currently the results is "yellow- green- yellow- red- green- green- red- green- yellow- green- R- B- B-" .
How can I convert it/change it to yellow-R- green-B- yellow-B- red-R- green-B- green-B- red-R- green-B- yellow-B- green-R- (random color - sequence -) As you can see, 10 random colors generated, but the sequence follows according.
Any answers or comments would be great as this is learning progress for me and others as well. Thank you.
CodePudding user response:
You can try this:
$color = array("green", "red", "yellow");
$sequence = array("R", "B", "B");
for ($x = 1; $x <= 10; $x ) {
foreach ($sequence as $sequence_value) {
$color_random = $color[array_rand($color)];
echo $color_random . "- " . $sequence_value . "- ";
}
}
CodePudding user response:
Each pair is a random thing from one list, coupled with the next thing from the other list. You're good with the random thing list, but on each loop you need to figure out which element in the sequence list to use.
There will only be one loop.
$color = array("green", "red", "yellow");
$sequence = array("R", "B", "B");
for ($x = 1; $x <= 10; $x ) {
$color_random = $color[array_rand($color)];
$sequence_index = ????
$sequenceValue = $sequence[$sequence_index];
echo $color_random . "- " . $sequenceValue . "- ";
}
So how do you find the right sequence index? There is more than one approach, but the simplest is the modulo operator. When x = 1
, you want the first item in the sequence list (index 0). When x = 2
, you want the second. When x = 4
, you want to loop back around and get index 0 again.
Modulo gives you the remainder when you divide two integers, so it goes in a cycle. x % 3
will almost give you what you want. (%
is the symbol for the modulo operator in php and many other languages)
The catch is that 1 % 3 = 1
so you will get index 1 when x = 1 (rather than the desired 0), and 3 % 3 = 0
, so you will get index = 0 when x = 3 (rather than the desired 3). The sequence is off by one from what you want.
The easiest way to fix this is make $x
go from 0 - 9, rather than 1 - 10. If x starts a 0, you're good:
$color = array("green", "red", "yellow");
$sequence = array("R", "B", "B");
for ($x = 0; $x <= 9; $x ) {
$color_random = $color[array_rand($color)];
$sequence_index = $x % 3;
$sequenceValue = $sequence[$sequence_index];
echo $color_random . "- " . $sequenceValue . "- ";
}
Alternately, if you have to start $x
at 1 for some other reason, you can use $sequence_index = ($x - 1) % 3
.
To make your code more resilient to change, the last thing is to adjust to cover if the length of the sequence array changes:
$sequence_index = $x % count($sequence);
so if you change the sequence you don't have to remember to update the loop, too.
CodePudding user response:
you dont need 2 loops or nested loops to create this sequence, I prefer to use %
to make it O(n)
complicity.
$color = array("green", "red", "yellow");
$sequence = array("R", "B", "B");
for($i = 0; $i < 10; $i ){
echo " {$color[array_rand($color)]} - {$sequence[$i%count($sequence)]} -";
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder
I hope it's helpful