Advertisement
Guest User

electionSimulator

a guest
Feb 22nd, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ElectionSimulator {
  4.  
  5. //Constants
  6. public static final int NUM_SIMS = 5;
  7. public static final int NUM_DISTS = 10;
  8. public static final double POLL_AVG = .52;
  9. public static final double POLL_ERR = .05;
  10.  
  11. public static void main(String[] args){
  12.  
  13. //Randomness creation
  14. Random rand = new Random();
  15.  
  16. //Welcome Message
  17. System.out.println("Welcome to the Election Simulator!");
  18. System.out.println("Running " + NUM_SIMS + " simulations of " + NUM_DISTS + " districts.");
  19. System.out.print("Our candidate is polling at " + (POLL_AVG * 100) + "% with a ");
  20. System.out.println((POLL_ERR * 100) + "% margin of error.");
  21. System.out.println("");
  22.  
  23. //Initialization
  24. int voteSum = 0; // total count of votes through all ten districts
  25. int myVote = 0; // total votes for candidate A
  26. int actualNum = 0; // actual num votes for each district
  27. int index = 0;
  28. int urVote = 0; // tots for cand B
  29. int turnOut1 = 0;
  30. double pollAcu = 0.0;
  31. double poll = 0.0;
  32. int plus = 0;
  33. int minus = 0;
  34. double totalAverage = 0;
  35.  
  36. //Simulation
  37. for(int i = 1; i <= NUM_SIMS; i++) {
  38. myVote = 0;
  39. urVote = 0;
  40. voteSum = 0;
  41.  
  42. for(int j = 1; j <= NUM_DISTS; j++) {
  43. turnOut1 = rand.nextInt(1000) + 1;
  44. pollAcu = rand.nextGaussian() * 0.5 * POLL_ERR;
  45. poll = pollAcu + POLL_AVG; //percentage voter A receives
  46. actualNum = turnOut1;
  47. myVote = myVote + (int)(poll * actualNum);
  48. voteSum = voteSum + turnOut1;
  49. urVote = urVote + (int)((1-poll) * actualNum);
  50. } //inner for for loop
  51.  
  52.  
  53. urVote = voteSum - myVote;
  54. double myPer = myVote / (double) voteSum;
  55. myPer = myPer * 100;
  56. totalAverage = totalAverage + myPer;
  57. boolean win = myPer >= 50.00;
  58. double roundedMyPer = (double) Math.round(myPer * 100) / 100;
  59. index = index + 1;
  60. plus = myVote / 100;
  61. minus = urVote / 100;
  62.  
  63.  
  64. System.out.println("Running simulation #" + index + ":"); //still need to make a for loop for the simulation number
  65. System.out.println(" Win? " + win);
  66. System.out.print(" Results: " + myVote + " (" + roundedMyPer + "%)");
  67. System.out.println(" - " + (urVote) + " " +"(" + (100 - roundedMyPer) + "%)");
  68. System.out.print(" Visualization: " );
  69. for(int k = 0; k < plus; k++){
  70. System.out.print("+");
  71. } // plus for loop end
  72. System.out.println("");
  73. System.out.print(" ");
  74. for(int o = 0; o < minus; o++){
  75. System.out.print("-");
  76. } // minus for loop end
  77. System.out.println("");
  78. } // outer for loop end
  79.  
  80. //Outro
  81. System.out.println();
  82. System.out.println("Average vote percentage: " + Math.round((double)(totalAverage/NUM_SIMS) * 100.0) / 100.0 + "%");
  83.  
  84. } // main method end
  85. } // public class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement