@bbcmicrobot hmph. Fine. I'll put rem statements in :)
@bbcmicrobot hmph. Fine. I'll put rem statements in :)
as per https://mathenchant.wordpress.com/2026/03/12/in-praise-of-stupid-questions/ and a @standupmaths #piday video, of course.
Also, the weird FOR L line seeds the RNG! Cheers to https://github.com/mattgodbolt/owlet-editor/issues/90 for that.
@sil @standupmaths calling RND with a negative number also seeds the RNG (though, from memory, I think the RNG has 3 bytes of state and the negative arg only sets 2 of them). A sneaky thing to do here would be to choose the seed so you "luckily" converge?
@bazzargh it does, but it seeds it _with_ that number, I think? So you'll always get the same values every run. (On a real computer RND does get seeded by clock ticks, but that's always the same in Owlet so you always get the same numbers.)
I ran @sil's program and got this.
Source: https://bbcmic.ro/?t=dOwUe #bbcbasic
@bbcmicrobot hmph. Fine. I'll put rem statements in :)
REM #bbcmicrobot Simulate coinflips to calculate pi 🚀
REM (easy to understand, I hope)
FOR L=13TO18: ?L=?-444: NEXT
C%=0
A=0
REPEAT
O=FNround(1)
C% = C% + 1
REM PRINT "Round", C%
REM PRINT "Game result", O
T=A * (C% - 1)
A=(T + O) / C%
PRINT "Pi after "; C%; " rounds is "; A * 4
UNTIL C% > 1000
END
DEF FNround(X)
REM flip coins until you've got more heads than tails, then return heads/flips
F%=0
H%=0
T%=0
REPEAT
R=RND(2)
IF R=1 THEN H% = H% + 1: ELSE T% = T% + 1
F% = F% + 1
UNTIL H% > T%
=H%/F%
@sil
Did you mean to overwrite 6 bytes in line 30? I'm not sure what reading -444 is doing. Removing the whole line gives you a 4x speedup.
RE: https://mastodon.social/@sil/116230068057924438
@geoffl it seeds the rng, apparently:
@sil The RNG seed only occupies 5 bytes from &D to &11 (13 to 17), location &12 (18) is the lower byte for TOP. But testing that code seems to cause a problems if more than the lower three bytes of the RNG seed are overwritten. Odd.
I ran @sil's program and got this.
Source: https://bbcmic.ro/?t=dOwXQ #bbcbasic
@bbcmicrobot Nice. 3.2ish, that's not a bad estimate for pi. i can live with that :)
#bbcmicrobot 🚀
REM @sil
DIM M 1024
FOR L=13TO17:?L=?-444: NEXT
FORL=M TOM+1023 STEP4:!L=RND:N.
C%=1:A=0
REPEAT
O=FNround(C%)
T=A*C%
C%=-NOTC%
A=(T+O)/C%
PRINT"Pi after "; C%; " rounds is "; A*4
UNTILC%>10000
END
DEFFNround(X%)
F%=0:H%=0:T%=0
REPEAT
R%=((M?((X%/8)MOD1023))/2^(X%MOD8))AND1
IFR%=1H%=-NOTH%ELSET%=-NOTT%
F%=-NOTF%:X%=-NOTX%
UNTILH%>T%
P.
=H%/F%
had to work out a (very, very simple) maths thing about rolling averages (how do you keep updating the average of a set of numbers as new numbers come in, without keeping the whole list around), which I solved with baby's first algebra, so I wrote it down because I was quite pleased to have worked it out even though everyone will say "duh yeah that's obvious" 😊 https://www.kryogenix.org/days/2026/03/14/calculating-a-rolling-average-without-keeping-all-the-numbers-around/