Probability of 5+ consecutive heads in 20 coin tosses

Discussion in 'Automated Trading' started by blueraincap, Oct 25, 2019.

  1. my bad, when i thought again, the sequential guessing implicitly makes P(right at i|wrong all before)=P(right at i) as wrong prior to i is a necessity rather than information for i event to happen.
    P(right by n) = [​IMG]
     
    #21     Nov 3, 2019
    elt894 likes this.
  2. For the record, the 1-100 guess game and its sim as follows

    Code:
    import java.util.Scanner;
    
    /**
    * Eck's book Chapter 4 PlayGame class
    * @version 1.0
    * @since 2019-11-1
    */
    
    public final class NumberGuessingGame{
    
      static int totalPlay;
      static int totalWin;
      static int guessPermitted = 6; //# of guess permitted per game
    
      public static void main(String[] args){
        System.out.println("This game is very easy.");
        System.out.println("I will pick a number between 1 and 100, and you guess it.");
        System.out.println("If you don't get it within "+ guessPermitted + " times, you are a fucking loser.");
        System.out.println("Now, let's rock and roll baby!");
        boolean playAgain;
        String playAgainString;
      
        do{
          System.out.println("I rolled my dice. Let's see if you get it.");
          totalPlay++;
          PlayNumberGuessingGame();
          System.out.println("Play again?");
          Scanner scanner = new Scanner(System.in);
          playAgainString = scanner.next();
          playAgainString = playAgainString.substring(0,1).toUpperCase();
          playAgain = (playAgainString.equals("Y")) ? true : false;
        } while(playAgain);
      
        double expectedWinRatio = NumberGuessingGameSim.NumberGuessingGameSim(guessPermitted);
        double actualWinRatio = (double)totalWin*100/totalPlay;
        String performance = (actualWinRatio>expectedWinRatio) ? ("outperformed") : ("underperformed");
        System.out.println("You have won " + totalWin + " games out of the total of " + totalPlay + " games played.");
        System.out.println("Your win ratio is " + String.format("%.1f", actualWinRatio) +"%, which " + performance + " the average of "+expectedWinRatio+"%");  
        System.out.println("Thanks for playing. Have a fucking good day!");
      }
    
      static void PlayNumberGuessingGame(){
        int answer, guess, guessCount;
        answer = (int)(Math.random() * 100) + 1;
        guessCount = 0;
        while(guessCount < guessPermitted){
          Scanner guessScan = new Scanner(System.in);
          System.out.println("What's your guess?");
          guess = guessScan.nextInt();
        
          if (guess == answer){
            break;
          }
          else if (guess < answer)
            System.out.println("Wrong, guess is too low.");
          else System.out.println("Wrong, guess is too high.");
          guessCount++;
        } //at this point, either guessCount has reached the quota or the guess is right
        if (guessCount == 6)
          System.out.println("Sorry, you lost. My dice number was " + answer + ".");
        else {
          totalWin++;
          System.out.println("Congratulations! You got it in " + (guessCount+1) + " guesses. What a genius.");}
      }
    }
    
        
                
    Code:
    import java.util.Scanner;
    
    public class NumberGuessingGameSim{
    
      static int totalWin = 0;
      static int totalRun = 100000;
    
      public static void main(String[] args){
        int guessPermitted;
        Scanner guessScan = new Scanner(System.in); //getting # of guesses allowed in each game
        System.out.println("Enter the max # of guesses per game");
        guessPermitted = guessScan.nextInt();
        double winRatio = NumberGuessingGameSim(guessPermitted);  
        System.out.println("win ratio is " + winRatio+"%");
      }
    
      public static double NumberGuessingGameSim(int guessPermitted){
        int currentRun = 0; //counter
        while(currentRun<totalRun){
          currentRun++;
          PlayNumberGuessingGameSim(guessPermitted);
        }
        return ((double)totalWin*100/totalRun);
      }
    
      private static void PlayNumberGuessingGameSim(int guessPermitted){
        int answer, guess, guessCount;
        int lowerRange, upperRange;
        answer = (int)(Math.random()*100) +1;
        guessCount = 0;
        lowerRange = 1;
        upperRange = 100;
        while (guessCount < guessPermitted){
          guess = (upperRange - lowerRange)/2 + lowerRange;
        
          if (guess == answer){
            totalWin++;
            break;
          }
          else if (guess < answer){
            lowerRange = guess;
            guessCount++;
          }
          else {
            upperRange = guess;
            guessCount++;
          }
        }
      }
    }
          
     
    #22     Nov 3, 2019
  3. Simulating the Monty Hall game.
    If someone can simplify the code, please comment.

    Code:
    import java.util.*;
    
    public final class MontyHallGameSim{
     
      static int totalSwitchWin = 0;
      static int totalKeepWin = 0;
     
      public static void main(String[] args){
       
        int totalPlay = 10000;
       
        for(int iplay=0; iplay<totalPlay; iplay++){
          playMontyHallGame();
        }
       
        System.out.println("Out of total of " + totalPlay + " games, stay(no-switch) strategy won " + totalKeepWin + " times and switch strategy won " + totalSwitchWin + " times.");
        System.out.println("Win ratios are respectively " + String.format("%.2f",(double)(totalKeepWin*100/totalPlay)) + "% and " + String.format("%.2f",(double)(totalSwitchWin*100/totalPlay)) + "%.");   
      }
           
        private static final void playMontyHallGame(){
        Set<Integer> doorsSet = new HashSet<Integer>(); //declare and initialize the doors
        doorsSet.add(1);
        doorsSet.add(2);
        doorsSet.add(3);
        Set<Integer> emptyDoorsSet = new HashSet<Integer>();
        emptyDoorsSet.addAll(doorsSet);
        int prizeDoor = (int)(Math.random()*3 + 1); //generate U[1,3] for the prize door
        emptyDoorsSet.remove(prizeDoor); //remove the prize door from the set
        int initialChoice = (int)(Math.random()*3 + 1) ;//generate U[1,3] for the player's initial choice
       
        emptyDoorsSet.remove(initialChoice); //preparing for openable door set
        ArrayList<Integer> openableDoorList = new ArrayList<Integer>(emptyDoorsSet); //an openable door is one that is both empty and not chosen
        int openableDoorShown = openableDoorList.get((int)(Math.random()*emptyDoorsSet.size())); //depending on whether initial choice is correct, the openable door set can have 1 (if incorrect) or 2 (if correct) elements
       
        int finalKeepChoice = initialChoice;
        int finalSwitchChoice;
       
        doorsSet.remove(initialChoice); //identifying the sole remaining door to switch to, by removing the initially chosen and the shown doors
        doorsSet.remove(openableDoorShown);
        if (doorsSet.contains(1))
            finalSwitchChoice = 1;
        else if (doorsSet.contains(2))
            finalSwitchChoice = 2;
        else
            finalSwitchChoice = 3;
       
        if(finalKeepChoice==prizeDoor)
          totalKeepWin++;
        else if (finalSwitchChoice==prizeDoor)
          totalSwitchWin++;
      }
       
    }
       
       
       
      
     
    #23     Nov 5, 2019