fork(1) download
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4.  
  5. class GameSimulator {
  6. public static void main(String[] args) {
  7. int playerCount = 1000; //100K
  8. int gameCount = 100000; //1M
  9. GameSimulator simulator = new GameSimulator(playerCount, gameCount);
  10. }
  11.  
  12. final int playerCount;
  13. final int gameCount;
  14. final Player[] playerList;
  15. ArrayList<Player> game;
  16.  
  17. public GameSimulator(int playerCount, int gameCount) {
  18. this.playerCount = playerCount;
  19. this.gameCount = gameCount;
  20.  
  21. playerList = new Player[playerCount];
  22. Random random = new Random();
  23.  
  24. for(int currentGame = 0; currentGame < gameCount; currentGame++) {
  25. game = new ArrayList<Player>();
  26. while(game.size() < 24) {
  27. //create a game of 24 players with no duplicates
  28. int playerId = random.nextInt(playerCount);
  29. if(!game.contains(getPlayer(playerId))) {
  30. game.add(getPlayer(playerId));
  31. }
  32. }
  33. int teamWins = random.nextInt(2);
  34. //Team 0 or Team 1 wins
  35. saveMatch(teamWins == 0);
  36. }
  37. //Print the distribution
  38. printWinLossBuckets();
  39. }
  40.  
  41. public void printWinLossBuckets() {
  42. //Get everyone's W/L
  43. final double[] winLoss = new double[playerCount];
  44. for(int i = 0; i < playerCount; i++) {
  45. getPlayer(i); //instantiate null players
  46. winLoss[i] = playerList[i].getWinRate(); //get win rate
  47. }
  48. Arrays.sort(winLoss);
  49. int bucketBracket = 0;
  50. int bucketCount = 0;
  51. double totalWinRate = 0;
  52. int totalGamesPlayed = 0;
  53. int mostGamesPlayed = 0;
  54.  
  55. for(int i = 0; i < playerCount; i++) {
  56. totalWinRate += winLoss[i]; //calculate average WR
  57. totalGamesPlayed += playerList[i].gamesPlayed; // calculate average games
  58. mostGamesPlayed = mostGamesPlayed < playerList[i].gamesPlayed ? playerList[i].gamesPlayed : mostGamesPlayed;
  59. while(winLoss[i] > bucketBracket) {
  60. if(bucketCount > 0) {
  61. System.out.println(bucketBracket + "\t" + bucketCount);
  62. }
  63. bucketBracket+=2;
  64. bucketCount = 0;
  65. }
  66. bucketCount++;
  67. }
  68. System.out.println("Average WR: " + (totalWinRate / (double) playerCount));
  69. System.out.println("Average Games Played: " + (totalGamesPlayed / (double) playerCount));
  70. System.out.println("Most Games Played by One Player: " + mostGamesPlayed);
  71. }
  72.  
  73. private void saveMatch(boolean firstTeamWins) {
  74. for(int i = 0; i < 12; i++) {
  75. game.get(i).playedGame(firstTeamWins);
  76. }
  77. for(int i = 12; i < 24; i++) {
  78. game.get(i).playedGame(!firstTeamWins);
  79. }
  80. }
  81.  
  82. public Player getPlayer(int id) {
  83. if(playerList[id] == null) {
  84. playerList[id] = new Player();
  85. }
  86. return playerList[id];
  87. }
  88.  
  89.  
  90. class Player {
  91. int gamesWon = 0;
  92. int gamesPlayed = 0;
  93. void lostGame() {
  94. gamesPlayed++;
  95. }
  96. void wonGame() {
  97. gamesPlayed++;
  98. gamesWon++;
  99. }
  100. void playedGame(boolean won) {
  101. if(won)
  102. wonGame();
  103. else
  104. lostGame();
  105. }
  106. double getWinRate() {
  107. return (double) gamesWon / (double) gamesPlayed * (double) 100;
  108. }
  109. }
  110. }
  111.  
Success #stdin #stdout 0.66s 320832KB
stdin
Standard input is empty
stdout
48	20
50	500
52	454
Average WR: 49.99995715978784
Average Games Played: 2400.0
Most Games Played by One Player: 2538