[ create a new paste ] login | about

Link: http://codepad.org/Vt3fFH3w    [ raw code | output | fork ]

PHP, pasted on Aug 24:
<?php


    function fillRandom($range, $num = 1) {
        $data = array_fill($range[0], $range[1]+1, FALSE);
        
        for ($i=0; $i<$num; $i++) {
            do {
                $key = mt_rand($range[0], $range[1]);
            } while ($data[$key] === TRUE);
            $data[$key] = TRUE;
        }
        return $data;
    }

$input = fillRandom(array(0, 5), 4);
ksort($input);
print_r($input);
printf("%d/%d\n", array_sum($input), count($input));

$input = fillRandom(array(0, 3), 2);
ksort($input);
print_r($input);
printf("%d/%d\n", array_sum($input), count($input));



/*** ***/

$array1 = array();
$array2 = array();
while (count($array1) < 4) {
    $rand = rand(1,6);
    if (!isset($array1[$rand])) {
         $array1[$rand] = TRUE;

    }
}

while (count($array2) < 2) {
    $rand = rand(1,4);
    if (!isset($array2[$rand])) {
        $array2[$rand] = TRUE;


    }
}


$array2 = $array2 + array_fill(1, 4, FALSE);
$array1 = $array1 + array_fill(1, 6, FALSE);

ksort($array1);
ksort($array2);

foreach($array1 as $k => $v){
    if ($v != 0)
        print "GOOD\n";
    else 
        print "BAD\n";
}

print "---\n";

foreach($array2 as $kc => $vc){
    if ($vc != 0)
        print "VERY GOOD\n";
    else 
        print "BAD\n";
}


Output:
Array
(
    [0] => 
    [1] => 1
    [2] => 
    [3] => 1
    [4] => 1
    [5] => 1
)
4/6
Array
(
    [0] => 
    [1] => 1
    [2] => 1
    [3] => 
)
2/4
BAD
GOOD
GOOD
BAD
GOOD
GOOD
---
BAD
BAD
VERY GOOD
VERY GOOD


Create a new paste based on this one


Comments: