Jump to content

The Law of Large Numbers, or Why It Is a Bad Idea to Go to the Casino


aum

Recommended Posts

A fairly simple way to get an answer to a task without having to analyze it mathematically is to simulate the task and see what happens. And this is best done with a computer program. These methods are also called Monte Carlo methods - in reference to the fact that one can also simulate gambling.

Coin flipping

If we want to know how often heads come out of 1000 coin tosses, we do it or better let the computer do it.

 

The computer program needs a random function that simulates the random landing of the coin.

 

random 2 returns an integer value between 1 and 2 inclusive, that is 1 or 2. 1 stands for heads and 2 for tails.

flip = random 2
if flip = 1
   print "Heads"
else
   print "Tails"
.
 
 

We flip a coin 1000 times and count how often heads come

n = 1000
for i = 1 to n
   flip = random 2
   if flip = 1
      heads += 1
   .
.
print "We got " & heads & " heads"
print "That is " & 100.0 * heads / n & "%"
 
 

We get roughly about 500 times head, so that's about 50% of all flips.


If we increase the number of flips to a million, the percentage of heads comes very close to 50%.

n = 1000000
for i = 1 to n
   flip = random 2
   if flip = 1
      heads += 1
   .
.
print "We got " & heads & " heads"
print "That is " & 100.0 * heads / n & "%"
 
 

This is called the Law of large numbers

Roulette

Now it's time to go to the casino - virtually with a software simulation.

 

We play roulette with a single zero wheel. Double zero wheels are twice as bad for the player. There are 37 numbers, from 0 to 36. We bet on the high numbers, i.e. we win the bet if the ball rolls to a number from 19 to 36, otherwise we lose our money.

 

So let's play, we'll bet $10:

numb = random 37 - 1
if numb >= 19
   print "You win $10"
else
   print "You lose $10"
.
 
 

Let's play a thousand times:

for i = 1 to 1000
   cash -= 10
   numb = random 37 - 1
   if numb >= 19
      cash += 20
   .
.
print "Cash after playing: $" & cash
 
 

Most of the time we lose. Why do we actually lose? The reason is the 0. There are 18 numbers where we win and 19 where we lose.


Now lets play 10000 times

for i = 1 to 10000
   cash -= 10
   numb = random 37 - 1
   if numb >= 19
      cash += 20
   .
.
print "Cash after playing: $" & cash
 
 

Now we lose nearly every time - That's the Law of large numbers

The simulation already comes close to the calculated value.

print 10 * 10000 * (18 / 37 - 19 / 37)
 
 

The new strategy: bet on 13 - we win $350 when the ball rolls to 13.

for i = 1 to 10000
   cash -= 10
   numb = random 37 - 1
   if numb = 13
      cash += 360
   .
.
print "Cash after playing: $" & cash
 
 

This doesn't change the chances much, only that the fluctuation margin is wider.

Gambling with $1000

We got $1000. We always bet $10 on the high numbers. When the $1000 is gone or becomes $2000, we stop playing. When the casino day is over (after 500 games), we go home and continue playing the next day.

cash = 1000
day = 1
while cash < 2000 and cash >= 10
   game += 1
   cash -= 10
   numb = random 37 - 1
   if numb >= 19
      cash += 20
   .
   if game = 500
      print "Day " & day & ": $" & cash
      day += 1
      game = 0
      sleep 1.5
   .
.
print "Day " & day & ": $" & cash
print "-------------"
 
 

We lose nearly every time our money.


We need a winning strategy. The chance that a high number will come after a low one could be higher. So we bet on the high numbers if there was a low number before, otherwise we bet on the low numbers.

cash = 1000
day = 1
while cash < 2000 and cash >= 10
   game += 1
   if numb < 19
      set_high = 1
   else
      set_high = 0
   .
   cash -= 10
   numb = random 37 - 1
   if set_high = 1 and numb >= 19
      cash += 20
   elif set_high = 0 and numb >= 1 and numb <= 18
      cash += 20
   .
   if game = 500
      print "Day " & day & ": $" & cash
      day += 1
      game = 0
      sleep 1.5
   .
.
print "Day " & day & ": $" & cash
print "-------------"
 
 

Sadly, that didn't work.


New Strategy: we double the bet after each loss so that the first win resumes all previous losses and makes a profit equal to the original bet. But we are limited by our capital and the table limit of $1000. Starting capital is $10000.

cash = 10000
day = 1
bet = 10
while cash < 20000 and cash >= 10
   game += 1
   cash -= bet
   numb = random 37 - 1
   if numb >= 19
      cash += 2 * bet
      bet = 10
   else
      bet *= 2
      if bet > 1000
         bet = 1000
      .
      if bet > cash
         bet = cash
      .
   .
   if game = 500
      print "Day " & day & ": $" & cash
      day += 1
      game = 0
      sleep 1.5
   .
.
print "Day " & day & ": $" & cash
print "-------------"
 
 

Most of the time we lose. But with this strategy we seem to be able to win more often. We'll just try this 100 times in a row and see how often it works out.

subr session
   cash = 10000
   bet = 10
   while cash < 20000 and cash >= 10
      cash -= bet
      numb = random 37 - 1
      if numb >= 19
         cash += 2 * bet
         bet = 10
      else
         bet *= 2
         if bet > 1000
            bet = 1000
         .
         if bet > cash
            bet = cash
         .
      .
   .
.
for i = 1 to 100
   call session
   if cash >= 20000
      n_win += 1
   .
.
print n_win
 
 

We win about 14 times in 100 tries with this strategy.


We haven't found a winning strategy for roulette. You can keep trying. Better on the computer than in a real casino. It is cheaper and at least you practice programming.

Lotto

Getting rich at the casino doesn't work so well. Let's try lotto, it doesn't make you poor as fast as the casino.

 

How many years does it take to have a Lotto six (6 out of 49) if you give a tip every week? For a Lotto five, the years are also printed out.

my_numbers[] = [ 3 7 13 27 31 49 ]
# 
len box[] 49
for i = 1 to 49
   box[i] = i
.
subr drawing
   for i = 1 to 6
      h = random (50 - i) + i - 1
      swap box[i] box[h]
   .
.
subr check
   n_match = 0
   for i = 1 to 6
      for j = 1 to 6
         if my_numbers[i] = box[j]
            n_match += 1
         .
      .
   .
.
while n_match < 6
   call drawing
   call check
   if n_match = 5
      print "5 correct after " & week div 52 & " years"
   .
   week += 1
.
print ""
print "6 correct after " & week div 52 & " years"
 
 

Your 6 numbers will come - sooner or later, more likely later.

Not only for gambling

There are other things you can do with Monte Carlo methods. For example, you can approximate the value of PI.

 

Fire random points at a square with side length 1 and count the number of points inside the quarter circle. This can be easily determined by the theorem of Pythagoras. This number times four is close to PI.

n = 100000
for i = 1 to n
   x = randomf
   y = randomf
   if x * x + y * y < 1
      hit += 1
   .
.
print "PI: " & hit / n * 4
 
 

With visualisation

n = 50000
for i = 1 to n
   x = randomf
   y = randomf
   if x * x + y * y < 1
      hit += 1
      color 900
   else
      color 000
   .
   move x * 100 y * 100
   circle 0.2
   if i mod 1000 = 0
      sleep 0.03
   .
.
print hit / n * 4
 
 

Blackjack

With blackjack, it's a completely different story.

 

Blackjack - Probabilities, Strategy, Card Counting

 


The examples were created with easylang.dev

[email protected]

 

Source

Link to comment
Share on other sites


  • 9 months later...
  • Replies 2
  • Views 507
  • Created
  • Last Reply

Top Posters In This Topic

  • aum

    1

  • tedrickpanrell

    1

  • tggenamho

    1

Top Posters In This Topic

tggenamho

It's a neat way to understand how outcomes stabilize over many trials. So, before hitting the casino, understanding these odds might save some coins!

Link to comment
Share on other sites


tedrickpanrell

Thanks for breaking down the Law of Large Numbers and Monte Carlo methods. It's fascinating how we can use simulations to understand probabilities, even in gambling scenarios. Reminds me of a time I was analyzing Manchester United's chances in a tough match.  Just like flipping coins, predicting outcomes in sports can be uncertain, but understanding the odds can give you an edge. It's all about finding patterns and making informed decisions.

Edited by tedrickpanrell
Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...