I have this array:
$numbers = array(1, 2, 3);
I can grab a random value from it like so:
$numbers[array_rand($numbers)];
But I need to come up with different random variations of these values. For example
1
13
123
3
32
12
3
12
13
231
etc...
As you can see a number can’t repeat more than once in each set, so we can’t have sets like:
113
232
33
etc...
How can this be done?
One possible solution is:
This both randomly decides to choose between 1-3 digits and can use the numbers 1-3 as many times as possible. Just change
$loopto the amount of times you want to run the generator.I also just realised you could simplify this further:
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 yourarray_rand()limit.