Skip to content

Instantly share code, notes, and snippets.

@JulienCabanes
Last active August 29, 2015 14:01
Show Gist options
  • Save JulienCabanes/13b424207705d8c3767e to your computer and use it in GitHub Desktop.
Save JulienCabanes/13b424207705d8c3767e to your computer and use it in GitHub Desktop.
tirage
<?php
function tirage_list($nb_tirage = 5, $list = array()) {
$result = array();
for($i = 1; $i <= $nb_tirage; $i++) {
$count = count($list);
if($count > 0) {
$result = array_merge($result, array_splice($list, rand(0, $count - 1), 1));
}
}
return $result;
}
function tirage_range($nb_tirage = 5, $min = 0, $max = 10, $scale = 1, $round = true) {
$values = array();
for($i = 1; $i <= $nb_tirage; $i++) {
$values[] = mt_rand($min, $max);
}
$sum = array_sum($values);
array_walk($values, function(&$value) use ($sum) {
$value = $value / $sum * 100;
});
if($round) {
array_walk($values, function(&$value) {
$value = round($value);
});
}
$sum = array_sum($values);
return $values;
}
echo '<pre>';
print_r(tirage_list(4, array(1, 2, 3, 4, 5)));
echo '<br/><br/>';
print_r(tirage_range(4, 0, 10, 100));
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment