Binomial distribution
Posted: Sun Jan 29, 2017 1:15 pm
Code: Select all
' binomial distribution
'
graphics ! graphics clear .8,.8,.8
dim balls(13)
get screen size w,h ! xo=w/2 ! randomize
r=10 ! d=2*r ! yl=800 ! nb=40
fill color 0,0,1 ! draw color 0,0,1
for i=0 to 10
for j=0 to i ! fill circle xo-i*d+2*j*d,100+2*i*d size r ! next j
next i
draw size 6 ! draw line xo-12*d,yl to xo+12*d,yl ! draw size 2
for i=-1 to 11 ! x=xo-2*(i-5)*d ! draw line x,yl to x,yl-280 ! next i
rb=.9*r
for i=1 to nb
dt=max(.1,.5/i)
fill color 1,0,0 ! fill circle xo,50 size rb ! pause .3
fill color .8,.8,.8 ! fill circle xo,50 size r ! pause .3
fill color 1,0,0 ! fill circle xo,100-d size rb ! pause .3
pos=12 ! x=xo ! y=100-d
for j=0 to 10
fill color .8,.8,.8 ! fill circle x,y size r
if rnd(1)<.5 then pos-=1 else pos+=1 ! x=xo-(12-pos)*d ! y+=2*d
fill color 1,0,0 ! fill circle x,y size rb ! pause dt
next j
fill color .8,.8,.8 ! fill circle x,y size r
k=(pos+1)/2 ! balls(k)+=1 ! y=yl-d*min(balls(k),13)
fill color 1,0,0 ! fill circle x,y size rb
next i
end