Guest User

Code for calculating chance to lose

a guest
Jun 20th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class mathwork {
  6.  
  7.  
  8.  
  9.  
  10. public static void main(String... args){
  11. double[][] input = new double[][] {
  12. new double[] {0.1, 4},
  13. new double[] {0.2, 3},
  14. new double[] {0.3, 2},
  15. new double[] {0.4, 1},
  16. };
  17. double Cost = 2.25;
  18. int NUMBER_OF_GAMES = 2;
  19. for(int NumberOfGames = NUMBER_OF_GAMES; NumberOfGames < NUMBER_OF_GAMES+1; NumberOfGames++){
  20. HashMap<Integer, Double> Initialmap = new HashMap<>();
  21. HashMap<Integer, Double> Dummymap = new HashMap<>();
  22. HashMap<Integer, Double> FinalMap = new HashMap<>();
  23. for(int i = 0; i < input.length; i++){
  24. Initialmap.put((int) Math.round(input[i][1]), input[i][0]);
  25. Dummymap.put((int) Math.round(input[i][1]), input[i][0]);
  26. }
  27. for(int i = 1; i < NumberOfGames; i++){
  28. for(int InitialKey : Initialmap.keySet()){
  29. for(int DummyKey : Dummymap.keySet()){
  30. int WinningValue = (int) Math.round(InitialKey + DummyKey);
  31. double WinningChance = Initialmap.get(InitialKey) * Dummymap.get(DummyKey);
  32. if(!FinalMap.containsKey(WinningValue)){
  33. FinalMap.put(WinningValue, WinningChance);
  34. }else{
  35. FinalMap.put(WinningValue, FinalMap.get(WinningValue) + WinningChance);
  36. }
  37. }
  38. }
  39. double InputCost = (i+1) * Cost;
  40. double WinningChance = 0;
  41. double LosingChance = 0;
  42. double EqualChance = 0;
  43. double ExpectedValue = 0;
  44. for(int FinalKey : FinalMap.keySet()){
  45. ExpectedValue += FinalMap.get(FinalKey) * FinalKey;
  46. if(FinalKey < InputCost){
  47. LosingChance += FinalMap.get(FinalKey);
  48. }else if(FinalKey == InputCost){
  49. EqualChance += FinalMap.get(FinalKey);
  50. }else{
  51. WinningChance += FinalMap.get(FinalKey);
  52. }
  53. }
  54.  
  55. System.out.println("For " + (i+1) + " games: " + LosingChance + " chance to lose, " + EqualChance + " chance to equal, " + WinningChance + " chance to win. expected Value: " + ExpectedValue);
  56.  
  57. Dummymap = new HashMap<>();
  58. for(int FinalKey : FinalMap.keySet()){
  59. Dummymap.put(FinalKey, FinalMap.get(FinalKey));
  60. }
  61. FinalMap = new HashMap<>();
  62. }
  63. }
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment