Put all forbidden numbers in an array and use array_difffrom range(1,400). You will get an array of allowed numbers, select random with array_rand().
<?php
$forbidden = array(2, 3, 6, 8);
$complete = range(1,10);
$allowed = array_diff($complete, $forbidden);
echo $allowed[array_rand($allowed)];
This way you remove the excluded numbers from the selection set and minimize the need for a loop :)
source
share