
Code: Select all
REM RndGauss -Test-
REM sB5.6/iOS6.1/iPhone4/by Operator
REM
GRAPHICS
GRAPHICS CLEAR 1,1,1
scr_w = SCREEN_WIDTH()
scr_h = SCREEN_HEIGHT()
DRAW COLOR 0,0,0
'set max. amount of bins
max_bins = 32
bin_width = scr_w/max_bins
DIM bins(max_bins)
'values to be normaly distributed
v_mean = 100
v_stdDev = 3.3
'range limits for the values
'used as "tolerances limits" here as
' +/- 10%
li_min = v_mean * 0.9
li_max = v_mean * 1.1
'limit_min = v_mean - 4*v_stdDev
'limit_max = v_mean + 4*v_stdDev
infoField()
FIELD "info" TEXT "Mean: "&v_mean&" StdDev: "&v_stdDev&" L-: "&li_min&" L+: "&li_max
LOOP:
i = rndGauss(v_mean,v_stdDev)
i = map(i,li_min,li_max,0,max_bins-1)
i = INT(i)
FILL COLOR 0.5,0.5,0.5
FILL ALPHA 1
'use "edge-bins" for values "out of" 'limit value ranges (MIN <-> MAX):
'red bins
IF i <= 0 THEN
i = 0
FILL COLOR 1,0,0
END IF
IF i >= max_bins-1 THEN
i = max_bins-1
FILL COLOR 1,0,0
END IF
bins(i) += 1
x1 = i*bin_width
y1 = scr_h - 20
x2 = i*bin_width + bin_width
y2 = y1 - bins(i)
FILL RECT x1,y1 TO x2,y2
DRAW RECT x1,y1 TO x2,y2
'circle strip below histogram
FILL COLOR 0,0,0
cir_r = 6
cir_x = i
cir_x =map(cir_x,0,max_bins-1,0,scr_w)
FILL ALPHA 0.01
FILL CIRCLE cir_x, scr_h-cir_r*1.5 SIZE cir_r
'rndGauss 2D mini plot
alf = RND(360)
r = rndGauss(0,15)
x = 60 + r*COS(alf)
y = 80 + r*SIN(alf)
FILL CIRCLE x,y SIZE 1
DRAW CIRCLE 60,80 SIZE 40
GOTO LOOP
DEF rndGauss(mean,stdDev)
'Standard NORMAL variate using
'Marsaglia polar method, code ported
'out of Wikipedia
'Subtraction to flip [0, 1) TO (0, 1]
WHILE q <= 0 OR q >= 1
u = 2 * RND(1) - 1
v = 2 * RND(1) - 1
q = u * u + v * v
END WHILE
p = SQR(-2 * LOG(q)/q)
'reset q value for next call
q = 0
RETURN mean + stdDev * p * u
END DEF
DEF map(var,min1,max1,min2,max2)
' range1: min1 max1
' range2: min2 max2
' value var in range1 is linearly
' mapped to a value var2 in range2
' var2 = min2+(var-min1)*(max2-min2)/(max1-min1)
RETURN (min2+(var-min1)*(max2-min2)/(max1-min1))
END DEF
DEF infoField()
FIELD "info" TEXT "" AT 0,0 SIZE .scr_w,25 RO
FIELD "info" BACK ALPHA 0.5
FIELD "info" FONT COLOR 0,0,0
END DEF