Skip to content

Instantly share code, notes, and snippets.

@RossMeikleham
Created February 23, 2015 20:30
Show Gist options
  • Save RossMeikleham/529267aef646002bf8af to your computer and use it in GitHub Desktop.
Save RossMeikleham/529267aef646002bf8af to your computer and use it in GitHub Desktop.
java alex
import java.util.Random;
public class BirthdaySimulation
{
public static void main(String [] args) {
Random rand = new Random();
int month = rand.nextInt(12);
int day = getDayInMonth(month);
int matches = 0;
// Check how many matches we get out of 100 random dates
for (int i = 0; i < 100; i++) {
int month2 = rand.nextInt(12);
int day2 = getDayInMonth(month);
// If month and day are equal, we have a match
if (month == month2 && day == day2) {
matches++;
}
}
System.out.println("We have " + matches + "for month:" + month + " day:" + day);
}
// Get number of days in month, assuming the current year isn't a leap year
public static int getDayInMonth(int month) {
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month > 11 || month < 0) {
return -1;
} else {
Random rand = new Random();
return rand.nextInt(monthDays[month]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment