## [En][Scala] Casino in Scala

Posted by Rizn on 09/03/2016

*The code aide is on GitHub for this article.*

A friend of mine took me to a casino to show one of her favourite pastimes (*hello Iwon Ka*). I reluctantly went with her to “make some money”. To get over with it, I played roulette and put £5 on “black” and lost it. Then I thought I could double my bet to £10 and if I win, then in total I will have £5 ( -5 + -10 + 20). And if I lose, then I could again double my bet to £20 and so on... until I win and have extra £5.

Of course I decided to stop after first bet as I was quite sure the casino has its way to make money.

After coming back home, I decided to write some code to see whether there's a way to win with casino by doubling your bet each time you lose.

Let's assume roulette has 18 blacks and 18 reds (that's not entirely true as there's one or two extra green places, but a bit later about it). It means there's 1/2 chance of having either black or red.

So here are the chances you will lose:

- If you play 1 time: (1/2)
^{1}= 1/2 - If you play 2 times (1/2)
^{2}= 1/4 - If you play 3 times (1/2)
^{3}= 1/8 - ...
- If you play 10 times (1/2)
^{10}= 1/1024 - …
- If you play 100 times (1/2)
^{100}= 1/1267650600228229401496703205376

So chances of losing drastically drop the more you play.

The only problem might be a limit of money you brought with you as you have to double bet each time. If you start with £5 and lose 9 consecutive times, then you need £2560 to play 10^{th} time just to reclaim your money and win £5 in total.

But if you lose 99 times in a row, then you'd need £3169126500570573503741758013440 to win £5 (probably spending time in casino wouldn't be your favourite thing if you had more money than the whole world altogether).

Here's the amount of money you need:

n = number of bets

m = initial bet

total = (2^{1-1} * m) + (2^{2-1} * m) + (2^{3-1} * m) + ... + (2^{n-1} * m)

- 2
^{0}* 5 = 5 - 2
^{1}* 5 = 10 - 2
^{2}* 5 = 20

To give it a go max. 3 times, you need £35, which gives you 7/8 chance to win £5 and 1/8 chance to lose £35.

So if you win £5*7 = £35 and lose 1*£35 then you have £0. Just waste of time really.

To proof the point, you can run this scala code.

If we run simulation:

- 1000 games
- max. 7 bets per game (7 bets as we assume we don't want to lose more than £1000 per game. Max. we can lose for 7 bets is £635 per game. If we wanted 8 bets, we'd need £1275).
- if you have budget for max. 7 bets, then it gives a chance 1/128 to lose £635 and chance 127/128 to win £5.

Let's play 10 times (in total 10*1000 = 10k games)

**+**£1160**-**£760**-**£3320**+**£1800**+**£3800**+**£520**+**£2440**-**£120**-**£2040**+**£1800

We've been a bit lucky this time, but the more you play, the mean will come closer to 0 (no win/no lose).

So if casino was fair and had the same amount of black and red and nothing more, then we could count on pure luck to win or lose using this system.

The real fun (for the casino) starts when you add to roulette a green slot. If green slot is chosen, you lose regardless if you selected black or red.

- 18 black + 18 red = 18/36 = 1/2 = 50% chance of winning
- 18 black + 18 red + 1 green = 18/37 = 48% chance of winning
- 18 black + 18 red + 2 green = 18/38 = 47% chance of winning

So what will happen if we play with 1 green using the same criteria (1000 games, max. 7 bets per game)?

**+**£1160**-**£120**-**£120**+**£1800**-**£2040**-**£1400**+**£1160**-**£3320**+**£760**-**£1400

As we see, this time we lost substantially more, however on occasion we were able to win something using our system.

What if casino has 2 green slots?

**-**£760**-**£3320**+**£1160**-**£3960**-**£1400**+**£520**-**£3960**-**£2040**+**£1160**-**£760

As expected the loses are more frequent and wins rarer.

Of course the more you play (with green slots) your loses widen.

To try this code with roulette with green slots, uncomment relevant option:

```
val roulette = RouletteOptions.black_18_and_red_18
//val roulette = RouletteOptions.black_18_and_red_18_and_1_green
//val roulette = RouletteOptions.black_18_and_red_18_and_2_green
```

What is the conclusion?

If you want to make money on roulette, then open a casino with roulette.