Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. static final int MAX_NUMBERS = 100;
  5. static final int GOAL = 100;
  6. static final int NUMBER_OF_RUNS = 1000;
  7.  
  8. static int current = 0;
  9. static int numMoves = 0;
  10.  
  11. public static void main(String[] args) {
  12. int totalNumMoves = 0;
  13. for (int i = 0; i < NUMBER_OF_RUNS; i++) {
  14. int numMovesForRun = run();
  15. totalNumMoves += numMovesForRun;
  16. }
  17.  
  18. double averageNumMoves = 1.0 * totalNumMoves / NUMBER_OF_RUNS;
  19. System.out.println("Average number of moves: " + averageNumMoves);
  20. }
  21.  
  22. private static int run() {
  23. current = 0;
  24. numMoves = 0;
  25.  
  26. while (current != GOAL) {
  27. if (shouldRandom()) {
  28. random();
  29. } else {
  30. next();
  31. }
  32. numMoves++;
  33. }
  34.  
  35. return numMoves;
  36. }
  37.  
  38. private static boolean shouldRandom() {
  39. // implement your strategy here
  40. double distanceToGoal = computeDistanceToGoal();
  41.  
  42. // where X is distance to goal
  43. double oddsOfGettingFartherAfterXRandoms = Math.pow(1 - distanceToGoal / MAX_NUMBERS, distanceToGoal);
  44. if (oddsOfGettingFartherAfterXRandoms < 1.0 * distanceToGoal / MAX_NUMBERS) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50.  
  51. private static void next() {
  52. current++;
  53. }
  54.  
  55. private static void random() {
  56. Random rng = new Random();
  57. current = rng.nextInt(100) + 1; // +1 beacuse Random starts at 0
  58. }
  59.  
  60. private static int computeDistanceToGoal() {
  61. if (GOAL > current) {
  62. return GOAL - current;
  63. } else {
  64. return MAX_NUMBERS - current + GOAL;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement