Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Created November 27, 2018 15:25
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 CompSciRocks/83ac93a976bc92eae316ff1928df078a to your computer and use it in GitHub Desktop.
Save CompSciRocks/83ac93a976bc92eae316ff1928df078a to your computer and use it in GitHub Desktop.
Solution for the Frog Simulation free response on the 2018 AP computer science exam - https://compsci.rocks
public double runSimulation(int num) {
int good = 0;
for (int i=0; i<num; i++)
if (simulate())
good++;
return (double)good / num;
}
public boolean simulate() {
int hops = 0;
int cntPos = 0;
while (hops < maxHops && cntPos >= 0 && cntPos < goalDistance) {
cntPos += hopDistance();
hops++;
}
return cntPos >= goalDistance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment