fork download
  1. <?php
  2. $nElementos = 20;
  3. $arr = array();
  4. $posibilidades = array(
  5. 1 => 40,
  6. 2 => 30,
  7. 3 => 20,
  8. 4 => 10
  9. );
  10.  
  11. while (count($arr) < $nElementos) {
  12. $n = rand(1,4);
  13. $p = rand(1,100);
  14. if ($p < $posibilidades[$n]) $arr[] = $n;
  15. }
  16.  
  17. print_r($arr);
  18. ?>
Success #stdin #stdout 0.01s 13112KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 2
    [4] => 1
    [5] => 3
    [6] => 1
    [7] => 3
    [8] => 3
    [9] => 1
    [10] => 3
    [11] => 1
    [12] => 1
    [13] => 1
    [14] => 2
    [15] => 4
    [16] => 1
    [17] => 3
    [18] => 1
    [19] => 1
)