How to calculate how many times it was derived and what is the result of the frequency of a particular element of the array.?

Here is my code:

$a = ["a","b","c","f","e","t","y","q","z","w"]; $b = count($a); for($i = 0; $i <= 10; ++$i){ $c = mt_rand(0,$b - 1); echo " Echod - \" " . strtoupper($a[$c])." \"<br>"; } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

From your code, you can think of something like this:

 $a = ["a","b","c","f","e","t","y","q","z","w"]; $b = count($a); $chances = []; $iterations = 10; for($i = 0; $i <= $iterations; ++$i){ $c = mt_rand(0, $b - 1); $chances[$a[$c]] = empty($chances[$a[$c]]) ? 1 : $chances[$a[$c]] + 1; echo " Echod - \" " . strtoupper($a[$c])." \"<br>"; } foreach($chances as $litera => $count) { echo $litera . ": " . ($count / $iterations * 100) . "%<br>"; } 
  • Thanks, helped! ;) - Andrew Stark