Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.comphenix.test;
- import java.util.Random;
- public class Application {
- public static void main(String[] args) {
- final Random rnd = new Random();
- final long total = 10000000;
- long victory = 0;
- for (int i = 0; i < total; i++) {
- int hpCharacters[] = new int[]{6, 5, 8, 1};
- victory += simulateAvengingWrath(hpCharacters, 3, rnd) ? 1 : 0;
- }
- System.out.printf("Probability of victory: %s%n", (victory / (double) total) * 100);
- }
- private static boolean simulateAvengingWrath(int[] hpCharacters, int enemyHeroIndex, Random rnd) {
- for (int i = 0; i < 8; i++) {
- int chosen = rnd.nextInt(hpCharacters.length);
- if (hpCharacters[chosen] > 0) {
- hpCharacters[chosen]--;
- } else {
- // Repeat and try another character
- i--;
- }
- // Did we just die? This also ensures we eventually exit this loop ...
- if (hpCharacters[enemyHeroIndex] <= 0) {
- return false;
- }
- }
- // Victory
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement