\ Simulation of batting slumps \ \ --------------------------------------------------- \ (c) Copyright 1999 Julian V. Noble. \ \ Permission is granted by the author to \ \ use this software for any application pro- \ \ vided this copyright notice is preserved. \ \ --------------------------------------------------- \ This is an ANS Forth program requiring the \ FLOAT, FLOAT EXT, FILE and TOOLS EXT wordsets. \ \ Environmental dependencies: \ Assumes independent floating point stack \ Assumes non-Standard graphics words from Win32Forth MARKER -slump include prng.f FVARIABLE p0 \ batting average 0.3e0 p0 F! \ initialize for safety 250 VALUE N \ # times @ bat/season 0 VALUE slump_len 10 VALUE max_len \ a slump is at least max_len hitless times @ bat 0 VALUE #slumps : initialize ( 0.1 seed 2!) 0 TO slump_len 0 TO #slumps ; : hit? ( -- f) prng p0 F@ F> NOT ; : check_slump ( -- f) \ is it a slump? max_len slump_len < ; : slump! check_slump \ was there a slump going? IF #slumps 1+ TO #slumps THEN 0 TO slump_len ; : measure ( -- ) N 0 DO hit? IF slump! ELSE slump_len 1+ TO slump_len THEN LOOP ( DROP) ; : stats ( ncases --) initialize DUP 0 DO measure LOOP CR . ." trials of " N . ." at-bats, giving " #slumps . ." slumps." ; FALSE [IF] Test case: test 522329230 ok \ You have to say TEST at least once \ to exercise the prng 10 stats 10 trials of 250 at-bats, giving 15 slumps. ok 10 stats 10 trials of 250 at-bats, giving 17 slumps. ok 10 stats 10 trials of 250 at-bats, giving 15 slumps. ok 10 stats 10 trials of 250 at-bats, giving 6 slumps. ok 10 stats 10 trials of 250 at-bats, giving 9 slumps. ok 10 stats 10 trials of 250 at-bats, giving 14 slumps. ok [THEN]