online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
''' Online Python Interpreter. Code, Compile, Run and Debug python program online. Write your code in this editor and press "Run" button to execute it. ''' import random """ We have three boxes which we cannot see into. Box(A) has two gold balls. Box(B) has a gold ball and a silver ball. Box(C) has two silver balls. We picked one of the three boxes at random and picked out a gold ball! We have 50% probability of having picked either box A or box B - just like a coin toss. We have 0% probability of having picked box C. Therefore, box C can be disregarded in our thought experiment. Two Box Scenario: What is the probability that when we reach in the same box that we randomly picked the first gold ball out of that we then pick another gold ball? Three Box Scenario: Just like the two box scenario, but without the assumption the we already picked one of the boxes with a gold ball in it. There is a probablity that we will pick the box with two silver balls. Which scenario is correct? """ #Boxes A B C boxList = [[0,0],[0,1],[1,1]] #0 = gold 1 = silver def ballPicker(): #randomly pick a ball from a position in the box. position = random.choice([0,1]) return position def boxPicker(boxes): #randomly pick a box from a list of boxes. box = random.choice(boxes) return box class scenario(object): #this class objects allows us to run different scenarios without using global variables. def __init__(self, name): self.goldBall = 0 self.silverBall = 0 self.totalPicked = 0 print("Running Scenario: ", name) def addGoldBall(self): self.goldBall += 1 return def addSilverBall(self): self.silverBall += 1 return def addTotalPicked(self): #we should only update total balls picked when picking the second ball. self.totalPicked += 1 return def probability(self): #output probabilities. print("Gold Ball Probability", self.goldBall / self.totalPicked, "with", self.totalPicked, "second balls picked.") print("Silver Ball Probability", self.silverBall / self.totalPicked, "with", self.totalPicked, "second balls picked.") return twoBoxes = scenario("Two Boxes") #Create scenario where we find the probability of the second gold ball where we have already picked one gold ball. #MainLoop for two boxes scenario. for i in range(0,1000000): box = boxPicker([0,1]) #randomly pick either box A or box B from which you already pulled a gold ball. if box == 0: #box A - automatically picks a gold ball - because it is the only ball remaining. twoBoxes.addGoldBall() twoBoxes.addTotalPicked() elif box == 1: #box B - automatically picks a silver ball - because it is the only ball remaining. twoBoxes.addSilverBall() twoBoxes.addTotalPicked() #Print the results. twoBoxes.probability() threeBoxes = scenario("Three Boxes") #Create a scenario where we find the probability of the second gold ball where we fail to pick a gold ball first. #Main loop for three boxes scenario. for i in range(0,1000000): box = list(boxList[boxPicker([0,1,2])])#pick box A B or C pos = ballPicker() #pick a random position in the box. if box[pos] == 0: #if the ball picked is gold, we will pick it and remove it from the box. #then we will pick the remaining ball from the box. box.pop(pos) if box[-1] == 0: #our second pick was a gold ball. Update the scenario. threeBoxes.addGoldBall() threeBoxes.addTotalPicked() elif box[-1] == 1: #our second pick was a silver ball. threeBoxes.addSilverBall() threeBoxes.addTotalPicked() elif box[pos] == 1: #our first pick was a silver ball there is nothing to record continue #Print the results. threeBoxes.probability()

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue