Çözüldü Monty Hall Simülasyonu kodundaki hata nerededir?

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

Sam Bridges

Hectopat
Katılım
6 Nisan 2020
Mesajlar
732
Makaleler
2
Çözümler
1
Yer
İzmir
Daha fazla  
Cinsiyet
Erkek
Merhaba arkadaşlar. Boş dururken aklıma Monty Hall problemi geldi ve bunun simülasyonunu yapayım dedim. Ancak, nedense oyuncu kapıyı değiştirdiğinde yaklaşık %40, değiştirmediğinde yaklaşık %100 winrate'e sahip oluyor. Problem kodun neresinde? Çok teşekkürler.

Python:
# Suppose you're on a game show, and you're given the choice of three doors:
# Behind one door is a car; behind the others, goats.
# You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door,
# say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?"
# Is it to your advantage to switch your choice?

import random

number_of_doors = 3
switch_status = False
winning_status = False


def game():
    global number_of_doors, switch_status, winning_status

    players_door = random.randint(1, number_of_doors)
    if players_door == 1:
        winning_status = True

    if switch_status is True:
        winning_status = not winning_status

    if winning_status is True:
        return 1
    else:
        return 0


def sim(redo_count):
    win_count = 0

    for i in range(redo_count):
        if game() == 1:
            win_count += 1

    print(f"winrate = {win_count/redo_count}")


sim(redo_count=100)
 
Çözüm
winning_status = False

bunu def game() içinde tanımlarsan sorun ortadan kalkar muhtemelen. yoksa bir kere true set olduktan sonra hep true olarak kalır, def game sürekli 1 return eder.
winning_status = False

bunu def game() içinde tanımlarsan sorun ortadan kalkar muhtemelen. yoksa bir kere true set olduktan sonra hep true olarak kalır, def game sürekli 1 return eder.
 
Çözüm
Winning_status = False

Bunu def game() içinde tanımlarsan sorun ortadan kalkar muhtemelen. yoksa bir kere true set olduktan sonra hep true olarak kalır, def game sürekli 1 return eder.

Doğru ya. Çok teşekkürler, nedense Python'u kullanmadığımda çok hızlı unutuyorum, global kullanmam bile bir sorun sanırım.

Çözüm (Optimal değil, yine global kullanıyorum )

Python:
# Suppose you're on a game show, and you're given the choice of three doors:
# Behind one door is a car; behind the others, goats.
# You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door,
# say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?"
# Is it to your advantage to switch your choice?

import random

number_of_doors = 3
switch_status = False  # Change this to True to see if it matters
winning_status = False


def game():
    global number_of_doors, switch_status, winning_status

    players_door = random.randint(1, number_of_doors)
    if players_door == 1:
        winning_status = True

    if switch_status is True:
        winning_status = not winning_status

    if winning_status is True:
        return 1
    else:
        return 0


def sim(redo_count):
    global winning_status
    win_count = 0

    for i in range(redo_count):
        winning_status = False
        if game() == 1:
            win_count += 1

    print(f"winrate = {win_count/redo_count}")


sim(redo_count=100)  # Make this count bigger to see if it matters (Law of Large Numbers)
 
Son düzenleme:

Bu konuyu görüntüleyen kullanıcılar

Bu siteyi kullanmak için çerezler gereklidir. Siteyi kullanmaya devam etmek için çerezleri kabul etmelisiniz. Daha Fazlasını Öğren.…