3v4l.org

run code in 500+ PHP versions simultaneously
<?php // $countAfter[$i][$j] will contain the number of occurrences of a pair $i, $j in the randomly generated sequence $countAfter = [1 => [1 => 0, 2 => 0], 2 => [1 => 0, 2 => 0] ]; $prev = 1; // We assume for simplicify that the "previously" generated value was 1 for ($i = 0; $i < 10000; $i++) { $n = mt_rand(1, 2); $countAfter[$prev][$n]++; $prev = $n; } print_r($countAfter);

preferences:
47.87 ms | 556 KiB | 5 Q