1

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
user1128811
  • 101
  • 5

4 Answers4

2

This solution lets you handle with any size of array...same operation..

    <?php
    $numbers = array(1, 2, 3);
    $count=count($numbers);
    $result="";
    $iterations=rand(1,$count);
    for($i=0;$i<$iterations;$i++)
    {
        $selected=$numbers[array_rand($numbers)];
        $numbers=remove_item_by_value($numbers,$selected);
        $result=$result.$selected;
    }


    function remove_item_by_value($array, $val) {
        foreach($array as $key => $value) {
        if ($value == $val)
           unset($array[$key]);
        }
        return $array;
        }

    echo $result;
    ?>

Now it will return you even random sized string:).

Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
1

Define the array, get a random length, shuffle the array, slice the array:

$numbers = array(1, 2, 3);
$length = rand(1, count($numbers));
shuffle($numbers);
$result = array_slice($numbers, -$length);

Demo

hakre
  • 193,403
  • 52
  • 435
  • 836
0

Well, define "random" (what are the chances of getting a 1-digit long string, or a 2-digit long, or a 3-digit long?), but here's one method:

$len = rand( 1, count( $numbers ) );
$result = '';
shuffle( $numbers );

for( $i = 0; $i < $len; ++$i ) {
    $result .= $numbers[ $i ];
}    

Note that this changes the original array. Make a copy of it if it shouldn't be changed.

JJJ
  • 32,902
  • 20
  • 89
  • 102
0

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.

George Reith
  • 13,132
  • 18
  • 79
  • 148
  • 2nd solution would work for me, but problem is it returns duplicate values, ex. 113 – user1128811 Jan 07 '12 at 10:04
  • @user1128811 The problem with anything random is that could happen. I will update with a way to ignore the duplicate numbers it generates. – George Reith Jan 07 '12 at 10:06
  • Oops just realized, that;s only because the $numbers array has duplicate values, changing that to just 1,2,3 sees to do it. – user1128811 Jan 07 '12 at 10:07