Created
November 2, 2022 08:10
-
-
Save AngleWyrm10/3107bf44f46ed4d13ce6aea5d75fb101 to your computer and use it in GitHub Desktop.
Simulation of exactly two successes with different P(success) each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class Scratchpad { | |
public static void main(String[] args) { | |
// set up our variables | |
Random rng = new Random(); | |
double pEvent1 = 0.1308; // P(success) of event 1 | |
double pEvent2 = 0.1623; // P(success) of event 2 | |
double pEvent3 = 0.1436 ; // P(success) of event 3 | |
int experiments = 10000; | |
int count = 0; | |
for(int thisTry = 0; thisTry < experiments; thisTry++){ | |
int successes = 0; | |
if(rng.nextDouble() <= pEvent1) successes++; | |
if(rng.nextDouble() <= pEvent2) successes++; | |
if(rng.nextDouble() <= pEvent3) successes++; | |
if(successes == 2) count++; | |
} | |
// display results | |
System.out.println( | |
String.format("Stats out of %s experiments\n%s had exactly 2 successes (%f)", | |
experiments, count, (float)count/experiments | |
) | |
); | |
} // main | |
} // Scratchpad |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment