-
-
Save stangirala/e610e28ad8c86dc52840ade4c13e9732 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class Main { | |
| public static void main(String[] args) { | |
| Histogram histogram = new Histogram(); | |
| histogram.sample(0); | |
| histogram.sample(10.0); | |
| } | |
| } | |
| // second file | |
| import java.util.LinkedList; | |
| import java.util.List; | |
| public class Histogram { | |
| private List<Integer> bins; | |
| private List<Integer> outputs; | |
| public Histogram() { | |
| bins = new LinkedList<>(); | |
| bins.add(0); | |
| bins.add(20); | |
| bins.add(60); | |
| bins.add(80); | |
| outputs = new LinkedList<>(); | |
| outputs.add(5); | |
| outputs.add(15); | |
| } | |
| public Integer sample(Double random) { | |
| Integer coinToss = (int) (random * 100); | |
| for (Integer bin : bins) { | |
| if (bin < coinToss) { | |
| return bin; | |
| } | |
| } | |
| return null; | |
| } | |
| public Integer sample(Integer random) { | |
| return sample(Math.random()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment