Skip to content

Instantly share code, notes, and snippets.

@joriki
Created February 11, 2023 08:43
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 joriki/5f0b29c3358f971678fa4383f91a53b7 to your computer and use it in GitHub Desktop.
Save joriki/5f0b29c3358f971678fa4383f91a53b7 to your computer and use it in GitHub Desktop.
Simulate an urn where two balls of the colour drawn are added after each draw; see https://math.stackexchange.com/questions/4636935.
package info.joriki.math.stackexchange;
public class Question4636935 {
final static int ntrials = 10000;
public static void main(String [] args) {
int count = 0;
int total = 0;
for (int n = 0;n < ntrials;n++) {
int nblack = 1;
int nred = 1;
boolean firstBlack = false;
for (int i = 0;i < 3;i++) {
boolean black = Math.random() < nblack / (double) (nblack + nred);
if (i == 0 && black)
firstBlack = true;
if (black)
nblack += 2;
else
nred += 2;
}
if (nblack <= 5) {
total++;
if (firstBlack)
count++;
}
}
System.out.println(count / (double) total);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment