Skip to content

Instantly share code, notes, and snippets.

@dphilipson
Created April 26, 2016 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dphilipson/334154011cba16479966041df720c9d0 to your computer and use it in GitHub Desktop.
Save dphilipson/334154011cba16479966041df720c9d0 to your computer and use it in GitHub Desktop.
Computes expected number of packs required to complete a portion of your collection in Hearthstone
package me.dphil.statistics
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* Many people have written this before and many will write it again. I
* wrote it because it was faster than looking for someone else's solution.
*/
public class PackStatistics {
private static final int NUM_BUCKETS = 36;
private static final int FULL_BUCKET_SIZE = 2;
private static final int REQUIRED_FULL_BUCKETS = 32;
private static final int NUM_SIMULATIONS = 100000;
public static void main(String[] args) {
long startTime = System.nanoTime();
int[] results = new int[NUM_SIMULATIONS];
for (int i = 0; i < NUM_SIMULATIONS; i++) {
results[i] = simulateOnce();
}
int sum = 0;
for (int i = 0; i < NUM_SIMULATIONS; i++) {
sum += results[i];
}
double mean = (double)sum / NUM_SIMULATIONS;
double squaredDifferenceSum = 0;
for (int i = 0; i < NUM_SIMULATIONS; i++) {
double difference = results[i] - mean;
squaredDifferenceSum += difference * difference;
}
double standardDeviation =
Math.sqrt(squaredDifferenceSum / NUM_SIMULATIONS);
long endTime = System.nanoTime();
System.out.println("Mean: " + mean);
System.out.println("Standard deviation: " + standardDeviation);
System.out.println("Computation time: "
+ TimeUnit.NANOSECONDS.toMillis(endTime - startTime) + "ms");
}
private static int simulateOnce() {
Random random = new Random();
int[] buckets = new int[NUM_BUCKETS];
int tries = 0;
int unfilledBuckets = REQUIRED_FULL_BUCKETS;
while (unfilledBuckets > 0) {
tries ++;
int nextBucket = random.nextInt(NUM_BUCKETS);
int bucketValue = ++buckets[nextBucket];
if (bucketValue == FULL_BUCKET_SIZE) {
unfilledBuckets--;
}
}
return tries;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment