samedi 27 octobre 2018

How do I check whether 3 candidates come last in any order with monte carlo

 /**Monte Carlo Simulation, to determine the probability of sean gallagher, 
  * perter casey and gavin duffy
  * finishing in last position in any order
  *
  * @author - Darragh Hackett
  * @version - 26/10/18
  *
  *
  *
  **/
    import java.util.*;

    public class electionMonteCarlo
    {
      public static void main(String args [])
      {
        String [] dobby = {"Michael D. Higgins", "Peter Casey", "Sean 
              Gallagher", "Liadh Ni Riada", "John Freeman", "Gavin Duffy"};
        ArrayList<String> elf = new ArrayList<>();
        fillArray(elf);
        System.out.println(elf);

        double counter = 0;

      }

       public static void fillArray(ArrayList<String> freeElf)
       {
         Scanner votes = new Scanner(System.in);
         System.out.println("Enter a number, to determine the number of 
         itterations");
         double vote_Itterations = votes.nextInt();
         Random probability  = new Random();
         ArrayList<String> sock = new ArrayList<>();
         double odds_random;

         for(int i = 0; i < vote_Itterations; i++)
         {
            odds_random = probability.nextDouble() * 1 + 0.0;

            if(odds_random > 0.0 && odds_random < 0.98)
            {
               sock.add("Michael D. Higgins");
               odds_random = probability.nextDouble() * 0.4 + 0.0;

               if(odds_random > 0.0 && odds_random < 0.0108)
               {
                 sock.add("Peter Casey");
                 odds_random = probability.nextDouble() * 0.29 + 0.0;

                 if(odds_random > 0.0 && odds_random < 0.016)
                 {
                   sock.add("Sean Gallagher");
                   odds_random = probability.nextDouble() * 0.26 + 0.0;
                 }
               }
              }
             }
              System.out.println(sock);
              String [] free = new String [sock.size()]; //create a new 
              array into which the elements of sock are stored,
              free = sock.toArray (free); //The toArray method copies am 
              ArrayList's elements into a typed array
              System.out.println(free);


          }
         }

So I need to figure out the probability that gallagher, duffy or casey will finish last in any order.

My code doesn't include the other candidates yet because I still need to work out the probability that they will be picked. So I set up nested if stateents so that if Higgins comes first then he's eliminated from placing second. But, my problem is that I don't know how to check if the three guys I mentioned will come last or not and that when I print the ArrayList that I converted to an Array Higgins is in it a lot but I know that's because he has the highest probability of winning.

I don't really understand the question because he just glanced over it but even the 3 demonstrators that I asked for help had trouble figuring it out. I got this far last night myself, but I'm still confused.

Aucun commentaire:

Enregistrer un commentaire