1
Next guys, I was studying how I could create a small draw application that was simple and fair and at the same time has probabilities, in my researches I found several ways to make a random choice of data stored inside an array, using the Rand(), mt_rand() and array_rand() commands and even using the shuffle() function to help shuffling.
That’s where the question comes in, to make a normal draw where the data has the same chance (Probability) to be chosen these functions by itself is enough and works very well, however I would like to know how I could add the element "Chance or Probability" that a data stored within the array or in the Database can be chosen
I will try to give an example of what I would like to try to do:
1 - Example that has equal chances:
<?php
//Variável que armazena os dados do array
$escolher = array("Arroz", "Feijão", "Macarrão", "Pizza", "Hamburguer");
//Variável que irá armazenar o elemento escolhido aleatóriamente
$resultado = array_rand($escolher);
//Imprimindo o Resultado na tela
echo 'O Alimento escolhido foi: ' . $resultado; //(Arroz)
?>
2 - Example that adds amount of odds or probabilities of an element to be chosen:
<?php
/*
Variável que armazena os dados do array,
repetindo os dados de acordo com o numero de chances que cada um tem de ser escolhido sendo,
Arroz = 2, Feijão = 1, Macarrão = 4, Pizza = 3, Hamburguer = 4
*/
$escolher = array(
"Arroz", "Arroz",
"Feijão",
"Macarrão", "Macarrão", "Macarrão", "Macarrão",
"Pizza", "Pizza", "Pizza",
"Hamburguer", "Hamburguer", "Hamburguer", "Hamburguer"
);
//Variável que irá armazenar o elemento escolhido aleatóriamente
$resultado = array_rand($escolher);
//Imprimindo o Resultado na tela
echo 'O Alimento escolhido foi: ' . $resultado; //(Macarrão)
?>
Well, the two examples work by returning a random result, but in the case I don’t know if the repetition that occurs in example 2 is reliable, and I would like to make the second example in a way where I don’t need to keep repeating the same information several times within the array to increase or decrease the chances of it being chosen, I would like to do something where I could-inform the probabilities through numbers, and inform what is the chance that each element has, and thus be able to manipulate who has the highest priority to be chosen and who has the lowest priority.
I was taking a look at these articles here from Sopt, but I was more lost than blind in gunfire, perhaps due to my level of knowledge that is not very high, so I would like your help, I thank you now.
These are the links to the articles I quoted:
The problem is confusing because he talks about weights, but there’s nothing there that should have weights, these are isolated items. If the problem is not well defined the solution will never be right, first set it correctly.
– Maniero
The array with the same key? This will never work
– novic
Weight I say is like odds, or chances. In case one item has more chances than another item to be chosen.
– JonatasM
I’m going to repeat what I said, nothing in your code indicates that you have what you’re saying, so it’s hard to give a proper solution, for example, if you had weights one being chosen over another the answer already given would be wrong, and it probably is and it didn’t help at all, so we should only answer when the question is clear.
– Maniero
Cool, I understand what you mean. I’ll try to rephrase the question.
– JonatasM