Skip to content

Instantly share code, notes, and snippets.

@atamrawi
Forked from benjholla/Puzzle1.java
Created September 8, 2018 07:30
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 atamrawi/3d279096091205ad02b0882d2382b221 to your computer and use it in GitHub Desktop.
Save atamrawi/3d279096091205ad02b0882d2382b221 to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment