         }
     }
 
+    public func minutesBetweenDate(startDate: NSDate, endDate: NSDate) -> Int
+    {
+        let calendar = NSCalendar.currentCalendar()
+        let components = calendar.components([.Minute], fromDate: startDate, toDate: endDate, options: [])
+        return components.minute
+    }
+    
     func nextSample(type: HKSampleType, asMale: Bool = true, date: NSDate, x: Double, y: Double?) -> HKSample? {
         switch type.identifier {
         case HKCategoryTypeIdentifierSleepAnalysis:

            HKQuantityTypeIdentifierPeripheralPerfusionIndex  : (80.0,   13.33),
            HKQuantityTypeIdentifierRespiratoryRate           : (6000.0, 1996.67),
            HKQuantityTypeIdentifierStepCount                 : (6000.0, 1996.67),
            HKQuantityTypeIdentifierUVExposure                : (6.0,    1.0)
        ],

        false: [

            let dv : [Double] = randNormals(dp.0, dp.1, count)
            let sv : [Double] = randNormals(sp.0, sp.1, count)
            return zip(ts, zip(dv, sv)).flatMap { (t,ds) in return completion(t, ds.0, ds.1) }
            
        case HKWorkoutTypeIdentifier:
            let tparams = eventTimeDistributions[asMale]![type.identifier]!
            let ts = sampleDailyTimestampsWithWeights(date, weights: tparams, count: count)
            let energy = parameters[asMale]![HKQuantityTypeIdentifierActiveEnergyBurned]!
            let distance = parameters[asMale]![HKQuantityTypeIdentifierDistanceWalkingRunning]!
            let ev : [Double] = randNormals(energy.0, energy.1, count)
            let dv : [Double] = randNormals(distance.0, distance.1, count)
            return zip(ts, zip(ev, dv)).flatMap { (t,ds) in return completion(t, ds.0, ds.1) }

        default:
            let tparams = eventTimeDistributions[asMale]![type.identifier] ?? defaultEventTimeDistribution
