3 door quiz problem sim

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

3 door quiz problem sim

Post by bluatigro »

this is a try at a proof that changing door is good
you shoot getr a 2x chance of getting thge car

error :\
i got strange results

Code: Select all

''you are in a quiz
''you have the main price
''you can choise between 3 doors
''behind 1 of the doors is a car
''behind the others is a goat
''you want the car
''you choise 1 of the doors
''the host opens a other door whit a goat
''he/she makes you choise again
''between the closed doors
''what do you do ?
''and why ?
function choise( a as integer , b as integer ) as integer
  dim as integer uit = a
  if rnd < .5 then uit = b
  return uit
end function
function other( a as integer , b as integer ) as integer
  dim as integer uit
  if a = 1 and b = 1 then uit = choise( 2 , 3 )
  if a = 1 and b = 2 then uit = 3
  if a = 1 and b = 3 then uit = 2
  if a = 2 and b = 1 then uit = 3
  if a = 2 and b = 2 then uit = choise( 1 , 3 )
  if a = 2 and b = 3 then uit = 1
  if a = 3 and b = 1 then uit = 2
  if a = 3 and b = 2 then uit = 1
  if a = 3 and b = 3 then uit = choise( 2 , 1 )
  return uit
end function
function dice( low as integer , high as integer ) as integer
  return int( rnd( 0 ) * ( high - low ) + low )
end function
dim as integer stay , change , door , door2 , car , k , i , host
stay = 0
change = 0
for i = 0 to 1000
  door = dice( 1 , 3 )
  car = dice( 1 , 3 )
  host = other( door , car )
  if i and 1 then
    door2 = other( door , host )
    k = 1
  else
    door2 = door
    k = 0
  end if
  if door2 = car then
    if k then
      change = change + 1
    else
      stay = stay + 1
    end if
  end if
next i
print "right when stay = " ; stay
print "right when change = " ; change
print "[ game over ]"
sleep
end
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: 3 door quiz problem sim

Post by bluatigro »

update :
error font and fixed
PROOF !!

so now you know what to do in a quiz whit this problem

Code: Select all

''you are in a quiz
''you have the main price
''you can choise between 3 doors
''behind 1 of the doors is a car
''behind the others is a goat
''you want the car
''you choise 1 of the doors
''the host opens a other door whit a goat
''he/she makes you choise again
''between the closed doors
''what do you do ?
''and why ?
function choise( a as integer , b as integer ) as integer
  dim as integer uit = a
  if rnd < .5 then uit = b
  return uit
end function
function other( a as integer , b as integer ) as integer
  dim as integer uit
  if a = 1 and b = 1 then uit = choise( 2 , 3 )
  if a = 1 and b = 2 then uit = 3
  if a = 1 and b = 3 then uit = 2
  if a = 2 and b = 1 then uit = 3
  if a = 2 and b = 2 then uit = choise( 1 , 3 )
  if a = 2 and b = 3 then uit = 1
  if a = 3 and b = 1 then uit = 2
  if a = 3 and b = 2 then uit = 1
  if a = 3 and b = 3 then uit = choise( 2 , 1 )
  return uit
end function
function dice( low as integer , high as integer ) as integer
  return int( rnd * ( high + 1 - low ) + low )
end function
dim as integer stay , change , door , door2 , car , k , i , host
stay = 0
change = 0
for i = 0 to 1000
  door = dice( 1 , 3 )
  car = dice( 1 , 3 )
  host = other( door , car )
  if i and 1 then
    door2 = other( door , host )
    k = 1
  else
    door2 = door
    k = 0
  end if
  if door2 = car then
    if k then
      change = change + 1
    else
      stay = stay + 1
    end if
  end if
next i
print "right when stay = " ; stay
print "right when change = " ; change
print "[ game over ]"
sleep
end
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: 3 door quiz problem sim

Post by BasicCoder2 »

https://betterexplained.com/articles/un ... l-problem/

Back in 2008 I wrote this:

Code: Select all

dim as double door(4) 
dim as integer cadillac
dim as integer choice
dim as integer reveal
dim as integer switch
dim as integer score1, score2

for i as integer =0 to 1000:'number of games 


   'put goats behind doors 
   door(0)=0 
   door(1)=0 
   door(2)=0 


   'change one to a Cadillac 
   cadillac = int(rnd(1)*3) 
   door(cadillac)=1 


   'pick a door 0, 1 or 2 
   choice = int(rnd(1)*3) 


   'reveal a goat 
   reveal = int(rnd(1)*3) 
   while reveal = choice or reveal = cadillac 
      reveal = int(rnd(1)*3) 
   wend 


   'switched choice 
   switch = int(rnd(1)*3) 
   while switch = choice or switch = reveal 
      switch = int(rnd(1)*3) 
   wend 


   'score cadillacs won with first choice 
   if door(cadillac) = door(choice) then 
      score1 = score1 + 1 
   end if 


   'score cadillacs won with switched choice 
   if door(switch) = door(cadillac) then 
      score2 = score2 + 1 
   end if 


next i 


print score1;score2 


sleep 
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: 3 door quiz problem sim

Post by Drago »

With fixed choosed door and random choice in second stage :)

Code: Select all

dim as double door(4)
dim as integer cadillac
dim as integer choice, choice2
dim as integer reveal, switch
dim as integer score1, score2, score3
dim as integer runtime

for runtime = 0 to 3

randomize timer
score1 = 0
score2 = 0
score3 = 0

for i as integer =0 to 100000:'number of games
   

   'put goats behind doors
   door(0)=0
   door(1)=0
   door(2)=0


   'change one to a Cadillac
   cadillac = int(rnd(1)*3)
   door(cadillac)=1


   'pick a door 0, 1 or 2
	if runtime = 3 then
		choice = int(rnd(1)*3)
	else 
		choice = runtime
	end if

   'reveal a goat
   reveal = int(rnd(1)*3)
   while reveal = choice or reveal = cadillac
      reveal = int(rnd(1)*3)
   wend

   'switched choice
   switch = int(rnd(1)*3)
   while switch = choice or switch = reveal
      switch = int(rnd(1)*3)
   wend

	'switch random
	choice2 = int(rnd(1)*3)
    while choice2 = reveal
		choice2 = int(rnd(1)*3)
	wend
	
	if door(cadillac) = door(choice2) then
		score3 = score3 +1
	end if

   'score cadillacs won with first choice
   if door(cadillac) = door(choice) then
      score1 = score1 + 1
   end if


   'score cadillacs won with switched choice
   if door(switch) = door(cadillac) then
      score2 = score2 + 1
   end if

next i

if runtime < 3 then
	print "Door      :"; runtime + 1
else
	print "Door      : Random"
endif
print "No  Switch:"; score1
print "    Switch:"; score2
print "Rnd Switch:"; score3
print "--------------------"

next runtime

sleep
Post Reply