Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Monty Hall Problem

Name: Anonymous 2011-08-29 6:51

Someone please tell me why this simulation is producing the same results for swapping and not swapping.


#include <iostream>
#include <algorithm>
#include <ctime>

const int steps = 10000000;

void monty_hall(bool switch_choice)
{
    unsigned long long wins = 0;
    int doors[3] = { 0, 1, 1 };
    int choice, reveal;
   
    for(int i = 0; i < steps; ++i)
    {
        std::random_shuffle(doors, doors + 3);
        choice = rand() % 3;
        reveal = rand() % 2;

        if(reveal)
            reveal = choice + 1;
        else
            reveal = choice - 1;

        if(reveal > 2)
            reveal = 0;
        else if(reveal < 0)
            reveal = 2;
       
        if(switch_choice)
        {
            if(choice != 0 && reveal != 0)
                choice = 0;
            else if(choice != 1 && reveal != 1)
                choice = 1;
            else
                choice = 2;
        }

        if(doors[choice] == 0)
            ++wins;
    }
    std::cout << wins;
}

int main()
{
    srand(time(NULL));
    std::cout << "Not switching: ";
    monty_hall(false);
    std::cout << "\n\nSwitching: ";
    monty_hall(true);
}

Name: Anonymous 2011-08-29 6:58

Nevermind I just noticed I wasn't making sure the reveal was a goat.

Name: !L33tUKZj5I 2011-08-29 6:59

Correct me if I'm wrong, but doesn't the this problem require that you already know what the first door will be? It's the probability of the second door that matters. So why assign three doors?

Name: Anonymous 2011-08-29 7:00

>>1
Is this thread even relevant to this board, or just your way of saying ``I can program in Sepples''?

Name: Anonymous 2011-08-29 7:01

Correct solution. You can all leave this thread now.


#include <iostream>
#include <algorithm>
#include <ctime>

const int steps = 10000000;

void monty_hall(bool switch_choice)
{
    unsigned long long wins = 0;
    int doors[3] = { 0, 1, 1 };
    int choice, reveal;
   
    for(int i = 0; i < steps; ++i)
    {
        std::random_shuffle(doors, doors + 3);
        choice = rand() % 3;
       
        for(int j = 0; j < 3; ++j)
        {
            if(j != choice && doors[j] == 1)
                reveal = j;
        }
       
        if(switch_choice)
        {
            if(choice != 0 && reveal != 0)
                choice = 0;
            else if(choice != 1 && reveal != 1)
                choice = 1;
            else
                choice = 2;
        }

        if(doors[choice] == 0)
            ++wins;
    }
    std::cout << wins;
}

int main()
{
    srand(time(NULL));
    std::cout << "Not switching: ";
    monty_hall(false);
    std::cout << "\n\nSwitching: ";
    monty_hall(true);
}

Name: !L33tUKZj5I 2011-08-29 7:01

In fact aren't you assigning four, lol. It's been a while since I have done any c++ and I can't remember whether variable arrays count up from zero first.

Name: Anonymous 2011-08-29 7:02

>>4
are you even relevant to this board, or was that just your way of saying ``im a jew''?

Name: Anonymous 2011-08-29 7:04

>>6
¯\(°_o)/¯

Name: SUDDENLY, JAVA 2011-08-29 7:31


import java.util.*;

public class Main {

    public static void main(String[] args) {
        final int MAX_ROUND = 9001;
        int win = 0;
        int lose = 0;
       
        for (int i = 0; i < (int)MAX_ROUND; i++) {
            Random rand = new Random();
            Round round = new Round();
            int selectedDoor = rand.nextInt(3);
           
            round.selectDoor(selectedDoor);
            round.openGoatDoor();
           
            /* comment this fucker so you can see the difference
                but it won't matter because there's already
                a fucking summary printed at the end of the program */
            round.change();
           
            Door door = round.openDoor();
            if (door.isCar()) {
                win++;
            } else {
                lose++;
            }
        }
       
        System.out.println("Rounds: "+ MAX_ROUND);
        System.out.println();
        System.out.println("Win:    "+ win);
        System.out.println("Loses:  "+ lose);
        System.out.println();
        System.out.println("Win Ratio: "+ round(win, MAX_ROUND) +"%");
        System.out.println("Lose Ratio: "+ round(lose, MAX_ROUND) +"%");
       
    }
   
    public static double round(double num, double den) {
        double number = num / den * 100;
        double zeros = Math.pow(10, 2);
        double value = Math.round(number * zeros) / zeros;
        return value;
    }
   
}

class Door {
   
    public static final int CAR = 0;
    public static final int GOAT = 1;
   
    public boolean open;
   
    private int value;
   
    public Door(int value) {
        this.value = value;
        open = false;
    }
   
    public boolean isOpen() {
        return open;
    }
   
    public void open() {
        open = true;
    }
   
    public boolean isCar() {
        return (value == CAR ? true : false);
    }
   
    public boolean isGoat() {
        return (value == GOAT ? true : false);
    }
   
}

class Round {
   
    private ArrayList<Door> doorList = new ArrayList<Door>();
   
    private int selectedDoor;
   
    public Round() {
        doorList.add(new Door(Door.CAR));
        doorList.add(new Door(Door.GOAT));
        doorList.add(new Door(Door.GOAT));
        Collections.shuffle(doorList);
    }
   
    public void selectDoor(int doorNumber) {
        selectedDoor = doorNumber;
    }
   
    public Door openDoor() {
        return doorList.get(selectedDoor);
    }
   
    public void openGoatDoor() {
        for (int i = 0; i < doorList.size(); i++) {
            if (selectedDoor != i) {
                Door door = doorList.get(i);
                if (door.isGoat()) {
                    door.open();
                }
            }
        }
    }
   
    public void change() {
        for (int i = 0; i < doorList.size(); i++) {
            Door door = doorList.get(i);
            if (!door.isOpen()) {
                if (selectedDoor != i) {
                    selectedDoor = i;
                }
            }
        }
    }
   
}

Name: Anonymous 2011-08-29 8:35

Why are you using C++ when you don't use any C++ features?

Name: Anonymous 2011-08-29 8:53

>>10
std
cout
for (int i
bool

Name: Anonymous 2011-08-29 9:02

>>11

Yeah he uses streams, which is the worse alternative to printf.

The rest is c99.

Name: Anonymous 2011-08-29 9:05

>>11
Expert BBCode master you are.

Name: Anonymous 2011-08-29 9:07

>>10
No, he isn't, for
he can't do multi-line quoting.

Name: Anonymous 2011-08-29 9:59

[b][i]MULTIQUOTE
MY
ANUS

Name: Anonymous 2011-08-29 10:01

FUCK
MY
AWFUL
BBCode
SKILLZ

Name: Anonymous 2011-08-29 10:13

{compile {quote {b.u.i.o MY ANUS}}

Name: Anonymous 2011-08-29 10:59

more like monty hall troll amirite?

Don't change these.
Name: Email:
Entire Thread Thread List