WeixiangYan/CodeTransOcean huggingface dataset:
Task: FizzBuzz






PHP: <?php
for ($i = 1; $i <= 100; $i++)
{
    if (!($i % 15))
        echo "FizzBuzz\n";
    else if (!($i % 3))
        echo "Fizz\n";
    else if (!($i % 5))
        echo "Buzz\n";
    else
        echo "$i\n";
}
?>






Ada: with Ada.Text_IO; use Ada.Text_IO;
 
procedure Fizzbuzz is
begin
   for I in 1..100 loop
      if I mod 15 = 0 then
         Put_Line("FizzBuzz");
      elsif I mod 5 = 0 then
         Put_Line("Buzz");
      elsif I mod 3 = 0 then
         Put_Line("Fizz");
      else
         Put_Line(Integer'Image(I));
      end if;
   end loop;
end Fizzbuzz;
Task: FizzBuzz






PHP: <?php
for ($i = 1; $i <= 100; $i++)
{
    if (!($i % 15))
        echo "FizzBuzz\n";
    else if (!($i % 3))
        echo "Fizz\n";
    else if (!($i % 5))
        echo "Buzz\n";
    else
        echo "$i\n";
}
?>




COBOL:       * FIZZBUZZ.COB
      * cobc -x -g FIZZBUZZ.COB
      *
       IDENTIFICATION        DIVISION.
       PROGRAM-ID.           fizzbuzz.
       DATA                  DIVISION.
       WORKING-STORAGE       SECTION.
       01 CNT      PIC 9(03) VALUE 1.
       01 REM      PIC 9(03) VALUE 0.
       01 QUOTIENT PIC 9(03) VALUE 0.
       PROCEDURE             DIVISION.
      *
       PERFORM UNTIL CNT > 100
         DIVIDE 15 INTO CNT GIVING QUOTIENT REMAINDER REM
         IF REM = 0
           THEN
             DISPLAY "FizzBuzz " WITH NO ADVANCING
           ELSE
             DIVIDE 3 INTO CNT GIVING QUOTIENT REMAINDER REM
             IF REM = 0
               THEN
                 DISPLAY "Fizz " WITH NO ADVANCING
               ELSE
                 DIVIDE 5 INTO CNT GIVING QUOTIENT REMAINDER REM
                 IF REM = 0
                   THEN
                     DISPLAY "Buzz " WITH NO ADVANCING
                   ELSE
                     DISPLAY CNT " " WITH NO ADVANCING
                 END-IF
             END-IF
         END-IF
         ADD 1 TO CNT
       END-PERFORM
       DISPLAY ""
       STOP RUN.
Task: Monty Hall problem



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        int switchWins = 0;
        int stayWins = 0;

        Random gen = new Random();
		    
        for(int plays = 0; plays < 1000000; plays++ )
        {
            int[] doors = {0,0,0};

            var winner = gen.Next(3);
            doors[winner] = 1; 
                
	    int choice = gen.Next(3); 
	    int shown; 
	    do
            {
	        shown = gen.Next(3);
	    }
            while (doors[shown] == 1 || shown == choice); 
     
	    stayWins += doors[choice]; 
     
            
            switchWins += doors[3 - choice - shown];
        }

        Console.Out.WriteLine("Staying wins " + stayWins + " times.");
        Console.Out.WriteLine("Switching wins " + switchWins + " times.");
    }
}









Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;

procedure Monty_Stats is
   Num_Iterations : Positive := 100000;
   type Action_Type is (Stay, Switch);
   type Prize_Type is (Goat, Pig, Car);
   type Door_Index is range 1..3;
   package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
   use Random_Prize;
   Seed : Generator;
   Doors : array(Door_Index) of Prize_Type;
   
   procedure Set_Prizes is
      Prize_Index : Door_Index;
      Booby_Prize : Prize_Type := Goat;
   begin
      Reset(Seed);
      Prize_Index := Random(Seed);
      Doors(Prize_Index) := Car;
      for I in Doors'range loop
         if I /= Prize_Index then
            Doors(I) := Booby_Prize;
            Booby_Prize := Prize_Type'Succ(Booby_Prize);
         end if;
      end loop;
   end Set_Prizes;
   
   function Play(Action : Action_Type) return Prize_Type is
      Chosen : Door_Index := Random(Seed);
      Monty : Door_Index;
   begin
      Set_Prizes;
      for I in Doors'range loop
         if I /= Chosen and Doors(I) /= Car then
            Monty := I;
         end if;
      end loop;
      if Action = Switch then
         for I in Doors'range loop
            if I /= Monty and I /= Chosen then
               Chosen := I;
               exit;
            end if;
         end loop;
      end if;
      return Doors(Chosen);
   end Play;
   Winners : Natural;
   Pct : Float;
begin
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Stay) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Stay : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Switch) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Switch : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");

end Monty_Stats;
Task: Monty Hall problem

C: 

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#define NumSim 1000000000 

void main() {
      unsigned long int i,stay=0;
      int ChosenDoor,WinningDoor;
      bool door[3]={0,0,0}; 
      
      srand(time(NULL));  
	 
	  for(i=0;i<=NumSim;i++){
	  
	      WinningDoor=rand() % 3; 
	  
	      ChosenDoor=rand() % 3;  
	  
	      if(door[WinningDoor]=true,door[ChosenDoor])stay++;
	  
	      door[WinningDoor]=false;
	  
      } 
	  
     printf("\nAfter %lu games, I won %u by staying.  That is %f%%. and I won by switching %lu That is %f%%",NumSim, stay, (float)stay*100.0/(float)i,abs(NumSim-stay),100-(float)stay*100.0/(float)i);
	
  }











Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;

procedure Monty_Stats is
   Num_Iterations : Positive := 100000;
   type Action_Type is (Stay, Switch);
   type Prize_Type is (Goat, Pig, Car);
   type Door_Index is range 1..3;
   package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
   use Random_Prize;
   Seed : Generator;
   Doors : array(Door_Index) of Prize_Type;
   
   procedure Set_Prizes is
      Prize_Index : Door_Index;
      Booby_Prize : Prize_Type := Goat;
   begin
      Reset(Seed);
      Prize_Index := Random(Seed);
      Doors(Prize_Index) := Car;
      for I in Doors'range loop
         if I /= Prize_Index then
            Doors(I) := Booby_Prize;
            Booby_Prize := Prize_Type'Succ(Booby_Prize);
         end if;
      end loop;
   end Set_Prizes;
   
   function Play(Action : Action_Type) return Prize_Type is
      Chosen : Door_Index := Random(Seed);
      Monty : Door_Index;
   begin
      Set_Prizes;
      for I in Doors'range loop
         if I /= Chosen and Doors(I) /= Car then
            Monty := I;
         end if;
      end loop;
      if Action = Switch then
         for I in Doors'range loop
            if I /= Monty and I /= Chosen then
               Chosen := I;
               exit;
            end if;
         end loop;
      end if;
      return Doors(Chosen);
   end Play;
   Winners : Natural;
   Pct : Float;
begin
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Stay) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Stay : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Switch) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Switch : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");

end Monty_Stats;
Task: Monty Hall problem




Go: package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	games := 100000
	r := rand.New(rand.NewSource(time.Now().UnixNano()))

	var switcherWins, keeperWins, shown int
	for i := 0; i < games; i++ {
		doors := []int{0, 0, 0}
		doors[r.Intn(3)] = 1 
		choice := r.Intn(3) 
		for shown = r.Intn(3); shown == choice || doors[shown] == 1; shown = r.Intn(3) {}
		switcherWins += doors[3 - choice - shown]
		keeperWins += doors[choice]
	}
	floatGames := float32(games)
	fmt.Printf("Switcher Wins: %d (%3.2f%%)\n",
		switcherWins, (float32(switcherWins) / floatGames * 100))
	fmt.Printf("Keeper Wins: %d (%3.2f%%)",
		keeperWins, (float32(keeperWins) / floatGames * 100))
}








Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;

procedure Monty_Stats is
   Num_Iterations : Positive := 100000;
   type Action_Type is (Stay, Switch);
   type Prize_Type is (Goat, Pig, Car);
   type Door_Index is range 1..3;
   package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
   use Random_Prize;
   Seed : Generator;
   Doors : array(Door_Index) of Prize_Type;
   
   procedure Set_Prizes is
      Prize_Index : Door_Index;
      Booby_Prize : Prize_Type := Goat;
   begin
      Reset(Seed);
      Prize_Index := Random(Seed);
      Doors(Prize_Index) := Car;
      for I in Doors'range loop
         if I /= Prize_Index then
            Doors(I) := Booby_Prize;
            Booby_Prize := Prize_Type'Succ(Booby_Prize);
         end if;
      end loop;
   end Set_Prizes;
   
   function Play(Action : Action_Type) return Prize_Type is
      Chosen : Door_Index := Random(Seed);
      Monty : Door_Index;
   begin
      Set_Prizes;
      for I in Doors'range loop
         if I /= Chosen and Doors(I) /= Car then
            Monty := I;
         end if;
      end loop;
      if Action = Switch then
         for I in Doors'range loop
            if I /= Monty and I /= Chosen then
               Chosen := I;
               exit;
            end if;
         end loop;
      end if;
      return Doors(Chosen);
   end Play;
   Winners : Natural;
   Pct : Float;
begin
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Stay) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Stay : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Switch) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Switch : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");

end Monty_Stats;
Task: Monty Hall problem





Java: import java.util.Random;
public class Monty{
	public static void main(String[] args){
		int switchWins = 0;
		int stayWins = 0;
		Random gen = new Random();
		for(int plays = 0;plays < 32768;plays++ ){
			int[] doors = {0,0,0};
			doors[gen.nextInt(3)] = 1;
			int choice = gen.nextInt(3); 
			int shown; 
			do{
				shown = gen.nextInt(3);
			
			}while(doors[shown] == 1 || shown == choice);
			
			stayWins += doors[choice];
			
			
			switchWins += doors[3 - choice - shown];
		}
		System.out.println("Switching wins " + switchWins + " times.");
		System.out.println("Staying wins " + stayWins + " times.");
	}
}







Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;

procedure Monty_Stats is
   Num_Iterations : Positive := 100000;
   type Action_Type is (Stay, Switch);
   type Prize_Type is (Goat, Pig, Car);
   type Door_Index is range 1..3;
   package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
   use Random_Prize;
   Seed : Generator;
   Doors : array(Door_Index) of Prize_Type;
   
   procedure Set_Prizes is
      Prize_Index : Door_Index;
      Booby_Prize : Prize_Type := Goat;
   begin
      Reset(Seed);
      Prize_Index := Random(Seed);
      Doors(Prize_Index) := Car;
      for I in Doors'range loop
         if I /= Prize_Index then
            Doors(I) := Booby_Prize;
            Booby_Prize := Prize_Type'Succ(Booby_Prize);
         end if;
      end loop;
   end Set_Prizes;
   
   function Play(Action : Action_Type) return Prize_Type is
      Chosen : Door_Index := Random(Seed);
      Monty : Door_Index;
   begin
      Set_Prizes;
      for I in Doors'range loop
         if I /= Chosen and Doors(I) /= Car then
            Monty := I;
         end if;
      end loop;
      if Action = Switch then
         for I in Doors'range loop
            if I /= Monty and I /= Chosen then
               Chosen := I;
               exit;
            end if;
         end loop;
      end if;
      return Doors(Chosen);
   end Play;
   Winners : Natural;
   Pct : Float;
begin
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Stay) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Stay : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Switch) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Switch : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");

end Monty_Stats;
Task: Monty Hall problem







Python: 
from random import randrange

doors, iterations = 3,100000  

def monty_hall(choice, switch=False, doorCount=doors):
  
  door = [False]*doorCount
  
  door[randrange(doorCount)] = True

  chosen = door[choice]

  unpicked = door
  del unpicked[choice]

  
  
  
  alternative = True in unpicked

  if switch:
    return alternative
  else:
    return chosen

print "\nMonty Hall problem simulation:"
print doors, "doors,", iterations, "iterations.\n"

print "Not switching allows you to win",
print sum(monty_hall(randrange(3), switch=False)
          for x in range(iterations)),
print "out of", iterations, "times."
print "Switching allows you to win",
print sum(monty_hall(randrange(3), switch=True)
          for x in range(iterations)),
print "out of", iterations, "times.\n"





Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;

procedure Monty_Stats is
   Num_Iterations : Positive := 100000;
   type Action_Type is (Stay, Switch);
   type Prize_Type is (Goat, Pig, Car);
   type Door_Index is range 1..3;
   package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
   use Random_Prize;
   Seed : Generator;
   Doors : array(Door_Index) of Prize_Type;
   
   procedure Set_Prizes is
      Prize_Index : Door_Index;
      Booby_Prize : Prize_Type := Goat;
   begin
      Reset(Seed);
      Prize_Index := Random(Seed);
      Doors(Prize_Index) := Car;
      for I in Doors'range loop
         if I /= Prize_Index then
            Doors(I) := Booby_Prize;
            Booby_Prize := Prize_Type'Succ(Booby_Prize);
         end if;
      end loop;
   end Set_Prizes;
   
   function Play(Action : Action_Type) return Prize_Type is
      Chosen : Door_Index := Random(Seed);
      Monty : Door_Index;
   begin
      Set_Prizes;
      for I in Doors'range loop
         if I /= Chosen and Doors(I) /= Car then
            Monty := I;
         end if;
      end loop;
      if Action = Switch then
         for I in Doors'range loop
            if I /= Monty and I /= Chosen then
               Chosen := I;
               exit;
            end if;
         end loop;
      end if;
      return Doors(Chosen);
   end Play;
   Winners : Natural;
   Pct : Float;
begin
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Stay) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Stay : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Switch) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Switch : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");

end Monty_Stats;
Task: Monty Hall problem

C: 

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#define NumSim 1000000000 

void main() {
      unsigned long int i,stay=0;
      int ChosenDoor,WinningDoor;
      bool door[3]={0,0,0}; 
      
      srand(time(NULL));  
	 
	  for(i=0;i<=NumSim;i++){
	  
	      WinningDoor=rand() % 3; 
	  
	      ChosenDoor=rand() % 3;  
	  
	      if(door[WinningDoor]=true,door[ChosenDoor])stay++;
	  
	      door[WinningDoor]=false;
	  
      } 
	  
     printf("\nAfter %lu games, I won %u by staying.  That is %f%%. and I won by switching %lu That is %f%%",NumSim, stay, (float)stay*100.0/(float)i,abs(NumSim-stay),100-(float)stay*100.0/(float)i);
	
  }
Task: Monty Hall problem



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        int switchWins = 0;
        int stayWins = 0;

        Random gen = new Random();
		    
        for(int plays = 0; plays < 1000000; plays++ )
        {
            int[] doors = {0,0,0};

            var winner = gen.Next(3);
            doors[winner] = 1; 
                
	    int choice = gen.Next(3); 
	    int shown; 
	    do
            {
	        shown = gen.Next(3);
	    }
            while (doors[shown] == 1 || shown == choice); 
     
	    stayWins += doors[choice]; 
     
            
            switchWins += doors[3 - choice - shown];
        }

        Console.Out.WriteLine("Staying wins " + stayWins + " times.");
        Console.Out.WriteLine("Switching wins " + switchWins + " times.");
    }
}
Task: Monty Hall problem


C++: #include <iostream>
#include <cstdlib>
#include <ctime>
 
int randint(int n)
{
  return (1.0*n*std::rand())/(1.0+RAND_MAX);
}

int other(int doorA, int doorB)
{
  int doorC;
  if (doorA == doorB)
  {
    doorC = randint(2);
    if (doorC >= doorA)
      ++doorC;
  }
  else
  {
    for (doorC = 0; doorC == doorA || doorC == doorB; ++doorC)
    {
      
    }
  }
  return doorC;
}

int check(int games, bool change)
{
  int win_count = 0;
  for (int game = 0; game < games; ++game)
  {
    int const winning_door = randint(3);
    int const original_choice = randint(3);
    int open_door = other(original_choice, winning_door);

    int const selected_door = change?
                                other(open_door, original_choice)
                              : original_choice;

    if (selected_door == winning_door)
      ++win_count;
  }
 
  return win_count;
}
 
int main()
{
  std::srand(std::time(0));

  int games = 10000;
  int wins_stay = check(games, false);
  int wins_change = check(games, true);
  std::cout << "staying: " << 100.0*wins_stay/games << "%, changing: " << 100.0*wins_change/games << "%\n";
}
Task: Monty Hall problem





Java: import java.util.Random;
public class Monty{
	public static void main(String[] args){
		int switchWins = 0;
		int stayWins = 0;
		Random gen = new Random();
		for(int plays = 0;plays < 32768;plays++ ){
			int[] doors = {0,0,0};
			doors[gen.nextInt(3)] = 1;
			int choice = gen.nextInt(3); 
			int shown; 
			do{
				shown = gen.nextInt(3);
			
			}while(doors[shown] == 1 || shown == choice);
			
			stayWins += doors[choice];
			
			
			switchWins += doors[3 - choice - shown];
		}
		System.out.println("Switching wins " + switchWins + " times.");
		System.out.println("Staying wins " + stayWins + " times.");
	}
}
Task: Monty Hall problem







Python: 
from random import randrange

doors, iterations = 3,100000  

def monty_hall(choice, switch=False, doorCount=doors):
  
  door = [False]*doorCount
  
  door[randrange(doorCount)] = True

  chosen = door[choice]

  unpicked = door
  del unpicked[choice]

  
  
  
  alternative = True in unpicked

  if switch:
    return alternative
  else:
    return chosen

print "\nMonty Hall problem simulation:"
print doors, "doors,", iterations, "iterations.\n"

print "Not switching allows you to win",
print sum(monty_hall(randrange(3), switch=False)
          for x in range(iterations)),
print "out of", iterations, "times."
print "Switching allows you to win",
print sum(monty_hall(randrange(3), switch=True)
          for x in range(iterations)),
print "out of", iterations, "times.\n"
Task: Monty Hall problem




Go: package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	games := 100000
	r := rand.New(rand.NewSource(time.Now().UnixNano()))

	var switcherWins, keeperWins, shown int
	for i := 0; i < games; i++ {
		doors := []int{0, 0, 0}
		doors[r.Intn(3)] = 1 
		choice := r.Intn(3) 
		for shown = r.Intn(3); shown == choice || doors[shown] == 1; shown = r.Intn(3) {}
		switcherWins += doors[3 - choice - shown]
		keeperWins += doors[choice]
	}
	floatGames := float32(games)
	fmt.Printf("Switcher Wins: %d (%3.2f%%)\n",
		switcherWins, (float32(switcherWins) / floatGames * 100))
	fmt.Printf("Keeper Wins: %d (%3.2f%%)",
		keeperWins, (float32(keeperWins) / floatGames * 100))
}
Task: Monty Hall problem



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        int switchWins = 0;
        int stayWins = 0;

        Random gen = new Random();
		    
        for(int plays = 0; plays < 1000000; plays++ )
        {
            int[] doors = {0,0,0};

            var winner = gen.Next(3);
            doors[winner] = 1; 
                
	    int choice = gen.Next(3); 
	    int shown; 
	    do
            {
	        shown = gen.Next(3);
	    }
            while (doors[shown] == 1 || shown == choice); 
     
	    stayWins += doors[choice]; 
     
            
            switchWins += doors[3 - choice - shown];
        }

        Console.Out.WriteLine("Staying wins " + stayWins + " times.");
        Console.Out.WriteLine("Switching wins " + switchWins + " times.");
    }
}








Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL