20
The reason for the question is study/learning. Know/apply programming techniques and concepts, transforming something "material" into "application".
I received a video on Whatsapp, where mixed colored balls were separated by colors passing through the pins. Or it was a lot of mechanical technology or fake video. In the end, it was a fake video, where a graphic design played a Board of Galton for a study job.
Wanting to know more, I found in Wikipedia, and would like to turn it into a "program".
The board basically simulates the "Central limit theorem" (link): The theorem describes the distribution of the mean of a random sample of a population with finite variance.
Idea:
Starting from the board, I decided to create a matrix, where I have malhas
and pinos
, forming a line:
-------Malha------->
[P1][P2][P3][P4][P5]...
Pin values:
Like the odds on the balls going down is always bigger from the center to the sides, I created the pins with values, and the bigger, more chances to "continue" there. But also, as the pins are "interspersed" and not vertically aligned, also considered this fact.
Knitting:
// Definicoes
$qtdMalhas = 15;
$qtdPinos = 15;
// Auxiliares malhas/pinos
$am = $ap = 0;
// Criando as malhas e seus pinos
while ($am <= $qtdMalhas) {
while ($ap < $qtdPinos) {
if ($ap < ($qtdPinos/2)) {
if ($am % 2 == 0) {
$m[$am][$ap] = $ap;
} else {
$m[$am][$ap] = $ap+1;
}
} else {
if ($am % 2 == 0) {
$m[$am][$ap] = $qtdPinos-$ap-1;
} else {
$m[$am][$ap] = $qtdPinos-$ap;
}
}
$ap++;
}
$ap = 0;
$am++;
}
Mesh structure:
Consider the matrix basically in the following format:
$m[0][0]-$m[0][1]-$m[0][2]-$m[0][3]-$m[0][4]...
$m[1][0]-$m[1][1]-$m[1][2]-$m[1][3]-$m[1][4]...
$m[2][0]-$m[2][1]-$m[2][2]-$m[2][3]-$m[2][4]...
$m[3][0]-$m[3][1]-$m[3][2]-$m[3][3]-$m[3][4]...
$m[4][0]-$m[4][1]-$m[4][2]-$m[4][3]-$m[4][4]...
...
Printing the fabric:
// Zera auxiliares malhas/pinos
$am = $ap = 0;
// Imprime a malha
while ($am < $qtdMalhas) {
echo '<br>|';
while ($ap < $qtdPinos) {
echo $m[$am][$ap].'|';
$ap++;
}
$ap = 0;
$am++;
}
Upshot:
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
|1|2|3|4|5|6|7|8|7|6|5|4|3|2|1|
|0|1|2|3|4|5|6|7|6|5|4|3|2|1|0|
Could make the mesh in other ways ?
Yes, using zero and one for the pins but I believe I would have more work to draw according to the probability of each pin. Among other ways.
With the mesh ready, how can I "pass the balls between her" ?
The idea would be to draw between the pin values below, whereas the odds of it staying ever closer to the higher value pin, that is, a draw with different odds, and thus obtain the result as the "Board of Galton".
Feel free to post your way of doing.
Whenever a ball hits a nail/pin, the chance of it falling to the left is 50% and to the right is 50%. This shows that your first figure has an error: it is impossible for the balls to reach the two gaps on the far left or the two on the far right, it would take two more rows of nails/pins for that.
– Victor Stafusa
So if you have
n
rows of pins, your approach to the problem becomes much simpler if you simply don
draws of numbers 0 or 1. Better yet, draw a random number, make a% (1 << n)
and then count how many bits 1 has in the number produced.– Victor Stafusa
So... as I even put more on the end, I could work with 0 and 1, having the probability 50% and 50% as I said, with the idea of her "going" to the side but being able to go back to the center. This way is just simplify the loops that create the matrix. Done this, I would make a
rand
of 2 number, and if the number is odd, I go to the left of the matrix, and if it is even, I go to the right. With this, the values ofarrays
would be the "column number" the ball will drop. That’s an idea with 0 and 1. That’s + - what you thought ?– rbz