sgezel / pronov2

0 stars 0 forks source link

Pickem haal random score op #2

Open sgezel opened 5 months ago

sgezel commented 5 months ago
<?php

function getRandom($values, $weights)
{
    $count = count($values); 
    $i = 0; 
    $n = 0; 
    $num = mt_rand(0, array_sum($weights) * 100) / 100; 
    while($i < $count)
    {
        $n += $weights[$i]; 
        if($n >= $num)
            break; 
        $i++; 
    } 
    return $values[$i]; 
}

$values = [1,2,3,4];
$weights = [60.0000,30.0000,9.9800,.0200];
$counts = array_combine($values, array_fill(0, count($values), 0));
for ($i = 0; $i < 100000; $i++) {
    $counts[getRandom($values, $weights)]++;
}

echo "value\tweight\tcount\t%\n";
for ($i = 0; $i < count($values); $i++) {
    echo $values[$i] . "\t" . $weights[$i] . "\t" . $counts[$values[$i]] . "\t" . round($counts[$values[$i]]/array_sum($counts)*100, 2) . "\n";
}
sgezel commented 5 months ago
Rijlabels Aantal van Thuis     Rijlabels Aantal van Uit
0 30,83%     0 28,57%
1 36,09%     1 37,59%
2 18,80%     2 21,80%
3 10,53%     3 6,77%
4 3,01%     4 3,76%
5 0,75%     5 1,50%
Eindtotaal 100,00%     Eindtotaal 100,00%
sgezel commented 5 months ago
Rijlabels Aantal van Kolom1
0 - 0 6,02%
0 - 1 11,28%
0 - 2 7,52%
0 - 3 3,01%
0 - 4 2,26%
0 - 5 0,75%
1 - 0 10,53%
1 - 1 15,04%
1 - 2 9,02%
1 - 3 0,75%
1 - 4 0,75%
2 - 0 6,77%
2 - 1 7,52%
2 - 2 2,26%
2 - 3 1,50%
2 - 4 0,75%
3 - 0 3,76%
3 - 1 3,01%
3 - 2 1,50%
3 - 3 1,50%
3 - 5 0,75%
4 - 0 1,50%
4 - 1 0,75%
4 - 2 0,75%
5 - 2 0,75%
(leeg) 0,00%
Eindtotaal 100,00%