<?php
"Item 1" => 0.5, // porcentagens
"Item 2" => 1,
"Item 3" => 4,
"Item 4" => 4.5,
"Item 5" => 5,
"Item 6" => 15,
"Item 7" => 15,
"Item 8" => 15,
"Item 9" => 20,
"Item 10" => 20
);
$valor = 1000; // valor do peso máximo e total dos itens
$inicio = 1;
$array_elementos = array(); // array para sorteio
// cria o array para sorteio
foreach($items as $nome => $porcentagem){
$val = ($valor * $porcentagem) / 100;
$valorFinal = $val + $inicio - 1;
$array_elementos[$nome] = $inicio."-".$valorFinal;
$inicio = $valorFinal + 1;
}
// numero randômico "sorteio"
// de 1 até o valor máximo do item mais "pesado"
$num_rand = rand(1, $valor);
$elemento_sorteado = "";
// procura o elemento sorteado
foreach($array_elementos as $nome => $peso){
if($num_rand >= $valores[0] && $num_rand <= $valores[1]){
$elemento_sorteado = $nome;
break;
}
}
echo $elemento_sorteado;
PD9waHAKCiRpdGVtcyA9IGFycmF5KAoKCSJJdGVtIDEiID0+IDAuNSwgLy8gcG9yY2VudGFnZW5zCgkiSXRlbSAyIiA9PiAxLAoJIkl0ZW0gMyIgPT4gNCwKCSJJdGVtIDQiID0+IDQuNSwKCSJJdGVtIDUiID0+IDUsCgkiSXRlbSA2IiA9PiAxNSwKCSJJdGVtIDciID0+IDE1LAoJIkl0ZW0gOCIgPT4gMTUsCgkiSXRlbSA5IiA9PiAyMCwKCSJJdGVtIDEwIiA9PiAyMAoKKTsKCiR2YWxvciA9IDEwMDA7IC8vIHZhbG9yIGRvIHBlc28gbcOheGltbyBlIHRvdGFsIGRvcyBpdGVucwokaW5pY2lvID0gMTsKCiRhcnJheV9lbGVtZW50b3MgPSBhcnJheSgpOyAvLyBhcnJheSBwYXJhIHNvcnRlaW8KCi8vIGNyaWEgbyBhcnJheSBwYXJhIHNvcnRlaW8KZm9yZWFjaCgkaXRlbXMgYXMgJG5vbWUgPT4gJHBvcmNlbnRhZ2VtKXsKCgkkdmFsID0gICgkdmFsb3IgKiAkcG9yY2VudGFnZW0pIC8gMTA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