Random Gauss -Test-

Post Reply
Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Random Gauss -Test-

Post by Operator »

3 small samples for rndGauss function :)

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

Attachments
image.jpg
image.jpg (86.87 KiB) Viewed 2559 times

User avatar
Dutchman
Posts: 860
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Random Gauss -Test-

Post by Dutchman »

Have you seen this:viewtopic.php?f=20&t=823 :D
Last edited by Dutchman on Thu Jan 19, 2023 3:32 pm, edited 1 time in total.

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Random Gauss -Test-

Post by Operator »

:shock: must have missed it, even I read
about every post in the forum. Guess because
I don't feel "comfortable" with i-numbers..

How could I (one) use your GRANDC function if i-numbers are not used...? Return only real part ?

User avatar
Dutchman
Posts: 860
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Random Gauss -Test-

Post by Dutchman »

Operator wrote:… How could I (one) use your GRANDC function if i-numbers are not used...? Return only real part ?
Use ABS(X) ;)
If X is a complex number, then ABS(X) returns the absolute value or modulus or magnitude.

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Random Gauss -Test-

Post by Operator »

:shock: that easy... Thanks for your feedback
and it's time for me to re-check those i-numbers...

Post Reply