# Vain attempts to win the lottery

Imagine an imaginary cunning uncle who wants to cheat and make money from "mugs". Let's call him Gennady Obmanuev.
On the most ordinary Tuesday, Gennady Obmanuev suddenly came up with a brilliant idea: to create a lottery in which each player himself can indicate his chance of winning and, consequently, the multiplier of winning and play on the rules set by him! In order to always remain in the black, Gennady at the end of each successful game takes a nominal fee of 5% of the winnings.

* if briefly about the game

As in the case of the casino, the longer a player plays such a game - the more likely he is to lose in the end. But is it really impossible to deceive the cunning uncle by inventing wonderful tactics, thanks to which you can increase your chances of winning?

First, let's try the standard tactics in any endeavor: a hundred thousand iterations with random values. On average, this will show the results of a huge mass of people who do not play systematically. Naturally, it is unreasonable to put a 100% interest rate with a five percent tax, so the rates will be limited from 10% (the minimum called by Gennady) to 94%.

I only know php, so I will write on it, I think it is understandable to everyone:
``````= \$random){
\$money += \$bet * 95/\$chance; //отдаем игроку :)
\$bank -= \$bet * 100/\$chance; //забираем у банка :)
}
}
echo floor(\$money-START_MONEY);
``````

The results were predictable, but not systematic due to the huge share of randomness in the game.
I ran this cycle five times, I got the results:

1. The player loses 225043.
2. The player loses 272766.
3. The player loses 320369.
4. The player loses 276055.
5. The player loses 254899.

The results look deplorable: our players owed the cunning uncle ~ 25 initial money. On average, players lose over 100,000 iterations of ~ 250,000 conventional units. Let's try to add logic to our algorithm. Naturally, the most reasonable would be to write
``````break;
``````

at the beginning of the cycle, but we are not looking for easy ways! Let's start by changing random values. Let's try to play with strictly set values. For example, a minimum bet on a maximum chance. What will such tactics lead to?
``````\$bet = 10;
\$chance = 94;
``````

As a result, we, of course, lose much less, because we bet less! Although you can see that the loss has become more predictable. It fluctuates around 50,000, with almost no variation.

1. Player loses 50940.
2. The player loses 50,900.
3. The player loses 51274.
4. The player loses 51041.
5. Player loses 49344.

The magnitude of the loss is not so important if we lose dozens of initial amounts and owe to the cunning Gennady. I specifically do not change the algorithm in the direction of “stopping on time", because I wonder if it is possible to get positive results after 100,000 iterations.

Let's try to play with a minimum bet and a minimum chance to win.
``````\$bet = 10;
\$chance = 10;
``````

Again we lose. This time the spread is very large, and this is logical: because we play with a minimal chance of victory.

1. The player loses 37650.
2. The player loses 58075.
3. The player loses 52660.
4. The player loses 43635.
5. Player loses 40310.

By the way, what’s interesting: if the minimum rate in the lottery would be 0.1%, then the results would become:

1. Player Wins 83000!
2. The player loses 78500.
3. The player loses 12,000.
4. Player Wins 7000!
5. The player loses 69,000.

The multiplier is so small that it increases our bet almost a thousand times, but this happens so infrequently that the results are completely unpredictable.

Let's try to apply some algorithm to win. For example: the more money we win, the riskier we can play. I implemented it this way:
``````\$bank = 1000000000; //банк
define('START_MONEY', 10000);
\$money = START_MONEY; //деньги игрока
for(\$i = 0; \$i < 100000; \$i++){
\$bet = 10;
\$pre_chance = 100 * (2 - 0.94 * \$money/START_MONEY); //94+ ставить нет смысла - забирает комиссия
if(\$pre_chance > 94){
\$chance = 0.94;
}else{
\$chance = \$pre_chance;
}
\$random = mt_rand(1, 1000)/10;
\$money -= \$bet;
\$bank += \$bet; //сразу отправляем деньги игрока в банк
if(\$chance >= \$random){
\$money += \$bet * 95/\$chance; //отдаем игроку :)
\$bank -= \$bet * 100/\$chance; //забираем у банка :)
}
}
echo floor(\$money-START_MONEY);
``````

* I hope this works.

And also it didn’t work out for me, the range of results was large, but in any case, I lost.

Then I tried to take serious measures by organizing an “inner pocket”. There, the player piles up money that he will never spend to stay with at least some pennies. I had to change the logic, because now there will be clearly less attempts than 100,000. And the most important thing I did: I set the goal for the player. For example, double the gain.
``````\$bank = 1000000000; //банк
define('START_MONEY', 10000);
\$money = START_MONEY; //деньги игрока
\$pocket = \$money/2;
\$money /= 2;
\$goal = START_MONEY*2;
for(\$i = 0; \$i < 100000; \$i++){
if(\$money < START_MONEY/10){
break; //доигрались...
}
if(\$money + \$pocket >= \$goal){
break; //наигрались!
}
\$bet = mt_rand(10, \$money/10); //минимальная ставка = 10.
\$chance = mt_rand(10, 94); //94+ ставить нет смысла - забирает комиссия
\$random = mt_rand(1, 100);
\$money -= \$bet;
\$bank += \$bet; //сразу отправляем деньги игрока в банк
if(\$chance >= \$random){
\$money += \$bet * 95/\$chance / 2; //половину отдаем игроку :)
\$pocket += \$bet * 95/\$chance / 2; //половину кладем в карман :)
\$bank -= \$bet * 100/\$chance; //забираем у банка :)
}
}
echo floor(\$money-START_MONEY);
``````

And only in this situation, with some probability, I began to win relatively small amounts. However, lose too.

##### Conclusion

In games where the percentage goes to the "institution" or the cunning Gennady Obmanuev, I could not win with a long game of a large number of people and I doubt that it is possible. In any case, due to the fact that events happen by chance, and the lottery takes the winnings in any case, it always remains in the black. The tax in this case fulfills the same goal as Zero on roulette in a casino: it shifts the overall chance of victory towards the institution, not the player.

Report errors in the text and in the code logic, please, in private messages. I will quickly fix it.

By the way, can someone come up with an algorithm that wins this lottery? It would be very interesting. Which algorithm is really the best in this game?