One possible solution is:
$number1 = array(1, 2, 3);
$number2 = array(1, 2, 3);
$number3 = array(1, 2, 3);
$loop = 10;
for($i=0;$i<$loop;$i++) {
$bool1 = mt_rand(0, 1);
$bool2 = mt_rand(0, 1);
$randomNumber = $number1[array_rand($number1)];
if($bool1)
$randomNumber .= $number2[array_rand($number2)];
if($bool2)
$randomNumber .= $number3[array_rand($number3)];
echo $randomNumber;
}
This both randomly decides to choose between 1-3 digits and can use the numbers 1-3 as many times as possible. Just change $loop
to the amount of times you want to run the generator.
I also just realised you could simplify this further:
$numbers = array(1, 1, 1, 2, 2, 2, 3, 3, 3);
$limit = mt_rand(1, 3);
$keys = array_rand($numbers, $limit);
$number = "";
if($limit == 1)
$number .= $numbers[$keys];
else {
foreach ($keys as $value) {
$number .= $numbers[$value];
}
}
Insert each number the maximum amount of times into the array you want it to appear in any number (maximum number of digits you want to generate). And randomly generate a number between 1-3 with mt_rand()
to use as your array_rand()
limit.