PHP: count how many times a random number appears in an array using a for loop? -
i trying simulate 2000 dice rolls of 11 sided die (number 2 through 12 on sides). have store data in array.
here's loop die rolled:
for($counter = 0; $counter <=2000; $counter++) { $die = rand(2, 12); $int[$counter] = $die; echo " $die ,"; }
^ seems work okay have 2000 random numbers output.
the next part have trouble. have output of results. make simple, let's have output number of fives rolled echo statement like:
"__ fives rolled."
i can't seem work. assignment have use loop. tried making new 1 if statement, , including if statement in loop above. no luck either. how can make work?
i think 1 solve problem..
$array = array(); for($counter = 0; $counter <=2000; $counter++) { $die = rand(2, 12); $array[]= $die; } for($c=2;$c<=12;$c++){ foreach($array $r){ if($c==$r) { $arr[$c]+=1; } } } foreach($arr $key=>$value) { echo $value." [".$key."] rolled<br>"; }
Comments
Post a Comment