Skip to content

Instantly share code, notes, and snippets.

@benjholla
Created August 24, 2018 21:05

Revisions

  1. benjholla created this gist Aug 24, 2018.
    23 changes: 23 additions & 0 deletions Puzzle1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import java.util.Random;

    public class Puzzle1 {

    public static void main(String[] args) {
    Random rnd = new Random();
    int odds = 0;
    int runs = 1000;
    for(int i=0; i<runs; i++) {
    int num = rnd.nextInt();
    if(isOdd(num)) {
    odds++;
    }
    }
    double oddPercentage = ((double) odds / (double) runs) * 100.0;
    System.out.println("Odd: " + String.format("%.2f", oddPercentage) + "%");
    }

    private static boolean isOdd(int num) {
    return num % 2 == 1;
    }

    }