Package exercises.projects

Source Code of exercises.projects.BirtdayParadoxTester

package exercises.projects;

import java.util.Calendar;
import java.util.GregorianCalendar;

import exercises.utils.MathHelpers;

public class BirtdayParadoxTester {
 
  public static double TestParadox( int sampleGroupSize, int numberOfTests ) {
   
    int success = 0;
   
    for( int i = 0; i < numberOfTests; i++ ) {
      if( hasEquivalentDates( sampleGroup( sampleGroupSize ) ) ) {
        ++success;
      }
    }
   
    return (double)( (double)success / (double)numberOfTests ) * 100.0;
  }
 
  public static Calendar getRandomDate() {
   
    int month = MathHelpers.randomBetween( 0, 11 );
    GregorianCalendar cal = new GregorianCalendar( 2000, month, 1 );
    int day = MathHelpers.randomBetween( 1, cal.getActualMaximum( GregorianCalendar.DAY_OF_MONTH ) );
    return new GregorianCalendar( 2000, month, day );
   
  }
 
  private static boolean hasEquivalentDates( Calendar[] dates ) {
   
    for (int i = 0; i < dates.length; i++) {
      Calendar date1 = dates[ i ];
      for (int j = i + 1; j < dates.length; j++) {
        if( date1.compareTo( dates[ j ] ) == 0 ) {
          return true;
        }
      }
    }
    return false;
   
  }
 
  private static Calendar[] sampleGroup( int size ) {
   
    Calendar[] dates = new Calendar[ size ];
   
    for( int i = 0; i < size; i++ ) {
      dates[i] = getRandomDate();
    }
   
    return dates;
  }

}
TOP

Related Classes of exercises.projects.BirtdayParadoxTester

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.