-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTLRand.m
More file actions
32 lines (23 loc) · 711 Bytes
/
TLRand.m
File metadata and controls
32 lines (23 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
// TLRand.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 9/23/09.
//
#import "TLRand.h"
#import "TLMersenneTwister.h"
#import <math.h>
#pragma mark -
@interface TLRand ()
@end
#pragma mark -
@implementation TLRand
+ (double)gaussianRandWithMean:(double)mean standardDeviation:(double)stddev min:(double)minValue max:(double)maxValue {
double theta = [TLMersenneTwister randRealClosed] * 2.0f * M_PI;
double radius = sqrt( -2.0f * log([TLMersenneTwister randRealClosed]) );
double randToNormalize = radius * cos(theta);
double normalizedRand = randToNormalize * stddev + mean;
double truncatedRand = MIN(maxValue, MAX(minValue, normalizedRand));
return truncatedRand;
}
@end