Instantly share code, notes, and snippets.

@benjholla /Puzzle1.java Secret
Created Aug 24, 2018

Embed
What would you like to do?
Java Puzzle 1 (spot the bug if one exists)
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;
}
}
@benjholla

This comment has been minimized.

Owner

benjholla commented Aug 29, 2018

Adapted from Java Puzzlers: Traps, Pitfalls, and Corner Cases - Puzzle #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment