• RANDOM routine pulled from an old paper

    From Hans Bezemer@21:1/5 to All on Wed Nov 8 01:46:31 2023
    Maybe it is useful to someone. It seems to work just fine. I have no Forth-79 compiler to verify it.

    : ARRAY CELLS ALLOT ;

    I think it's quite obvious but anyways. The curious part is that the author seems to prefer 1-indexed arrays instead of 0-indexed ones.

    Hans Bezemer

    \ A Portable FORTH Random Number Generator, William T. Doyle
    \ The Journal of Forth Application and Research Vol. I. Number 2. Dec. 1983

    \ When seeded with the word RND.INIT the sequences produced are identical
    \ to those produced by Knuth's FORTRAN subroutine with the same seed and
    \ number range.

    \ ANS conversion by Hans Bezemer, 2023
    \ Converted to a single word version

    ( RANDOM NUMBER GENERATOR FORTH-79 WTD)

    55 ARRAY RND DOES> SWAP 1- CELLS + ; ( 55 RANDOM NRS IN RND )

    VARIABLE TALLY 0 TALLY !
    VARIABLE SEED 314159296 SEED !

    : CYCLE ( N I A --- N2)
    DUP >R @ 1+ DUP ROT > IF DROP 1 THEN DUP R> ! ;

    : STEP ( A --- N+ I)
    55 TALLY CYCLE ;

    ( RANDOM NUMBER GENERATOR 2 )
    : RND.INIT ( --- )
    0 TALLY ! SEED @ DUP 55 RND !
    1 SWAP 55 1 DO OVER DUP 21
    I * 55 MOD RND ! - DUP 0< IF
    1000000000 + THEN SWAP LOOP
    DROP DROP ;

    : RANDOM ( --- N)
    STEP DUP >R RND @ DUP R@ DUP
    25 < IF 31 + ELSE 24 - THEN RND
    @ - DUP 0< IF 1000000000 +
    THEN R> RND ! ;

    ( RANDOM NUMBER GENERATOR 3)
    : WARMUP ( --- )
    221 1 DO RANDOM DROP LOOP ;
    : RANDOMIZE ( N --- )
    TALLY ! RND.INIT WARMUP ;

    0 randomize random . cr depth .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to Hans Bezemer on Wed Nov 8 12:59:02 2023
    Hans Bezemer wrote:

    55 ARRAY RND DOES> SWAP 1- CELLS + ; ( 55 RANDOM NRS IN RND )

    This DOES> must either have defined interpretation semantics
    or something else is missing here...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to mhx on Thu Nov 9 10:30:35 2023
    mhx wrote:
    Well, from the posting's comments one might think it is a truthful
    copy of the code in the original paper, for 16-bit Forth79.

    Aah, Forth folklore and nostalgia.. ;-)

    Assuming William Boyle did not have a 64-bit Forth79 compiler,
    "314159296" and "1000000000" differ from the original critical
    constants. This makes it doubtful the generator has the
    statistical properties Knuth intended. (Given that William
    Boyd converted Knuth's Fortran code correctly! Was there ever
    a Fortran with a 16-bit integer RND function?)

    I have Shishua in my (non-standard Forth) libraries: https://espadrine.github.io/blog/posts/shishua-the-fastest-prng-in-the-world.html
    (obviously not in MinForth)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxf@21:1/5 to Hans Bezemer on Thu Nov 9 22:40:44 2023
    On 9/11/2023 3:32 am, Hans Bezemer wrote:
    On Wednesday, November 8, 2023 at 2:02:32 PM UTC+1, minforth wrote:
    Hans Bezemer wrote:

    55 ARRAY RND DOES> SWAP 1- CELLS + ; ( 55 RANDOM NRS IN RND )
    This DOES> must either have defined interpretation semantics
    or something else is missing here...

    Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
    Type `help' for basic help
    : array create cells allot ; ok
    55 ARRAY RND DOES> SWAP 1- CELLS + ; ok
    25 1 rnd ! ok

    Works fine.. As Bernd said "It's trivial to add interpretation semantics.. So why not?"

    But if you're happier with ": ARRAY CREATE CELLS ALLOT DOES> SWAP 1- CELLS + ;"
    then go for it. I told you: "some assembly required"..

    Alternatively

    :NONAME DOES> SWAP 1- CELLS + ; ( xt)
    CREATE RND 55 CELLS ALLOT
    ( xt) EXECUTE

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxf@21:1/5 to minforth on Fri Nov 10 09:42:05 2023
    On 9/11/2023 3:58 am, minforth wrote:

    I like my interpretative DO..LOOP or better FOR..NEXT as well.

    What is that?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hans Bezemer@21:1/5 to dxf on Fri Nov 10 01:23:00 2023
    On Thursday, November 9, 2023 at 11:42:14 PM UTC+1, dxf wrote:
    On 9/11/2023 3:58 am, minforth wrote:

    I like my interpretative DO..LOOP or better FOR..NEXT as well.
    What is that?
    Since everything in 4tH is compiled, you can do:

    20 0 ?DO I . LOOP

    *outside* of a definition. I've also seen Forths that have [DO] and [LOOP]
    as interpreted equivalents. I suppose you could embed those within a regular DO..LOOP in order to create "interpretative DO..LOOP".

    In 4tH code like:

    10 0 DO variable i LOOP

    doesn't create a bunch of variables. As a matter of fact, it will not compile because "I" as a name is taken. You could do that in the preprocessor though, although the code to achieve this will not look like this - at all.

    I suppose you could do something like:

    10 0 [DO] variable var i & [LOOP]

    in an interpreted version .. but here I'm just guessing.

    Hans Bezemer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to the.beez.speaks@gmail.com on Fri Nov 10 11:37:26 2023
    In article <ec1b613d-cf8e-4bf0-ba73-eab288a15089n@googlegroups.com>,
    Hans Bezemer <the.beez.speaks@gmail.com> wrote:
    On Thursday, November 9, 2023 at 11:42:14 PM UTC+1, dxf wrote:
    On 9/11/2023 3:58 am, minforth wrote:

    I like my interpretative DO..LOOP or better FOR..NEXT as well.
    What is that?
    Since everything in 4tH is compiled, you can do:

    20 0 ?DO I . LOOP

    *outside* of a definition. I've also seen Forths that have [DO] and [LOOP]
    as interpreted equivalents. I suppose you could embed those within a regular >DO..LOOP in order to create "interpretative DO..LOOP".

    In 4tH code like:

    10 0 DO variable i LOOP

    doesn't create a bunch of variables. As a matter of fact, it will not compile >because "I" as a name is taken. You could do that in the preprocessor though, >although the code to achieve this will not look like this - at all.

    I suppose you could do something like:

    10 0 [DO] variable var i & [LOOP]

    in an interpreted version .. but here I'm just guessing.

    After (in ciforth)
    WANT -scripting-
    you can do *all* control construct in interpreter mode.
    (It is implicit in the -s option, for scripting)

    T] and T[ switch to a temporary compile area. It is typically 4 Gbyte
    away in a 8 Gbyte Forth, but beware of large buffers. (The same
    mechanism is used for classes.)

    Then it is one screen worth of definitions:

    --------------------------------------------
    "T]" WANTED
    : IF T] POSTPONE IF ; IMMEDIATE
    : DO T] POSTPONE DO ; IMMEDIATE
    : ?DO T] POSTPONE ?DO ; IMMEDIATE
    : BEGIN T] POSTPONE BEGIN ; IMMEDIATE
    : THEN POSTPONE THEN POSTPONE T[ ; IMMEDIATE
    : LOOP POSTPONE LOOP POSTPONE T[ ; IMMEDIATE
    : +LOOP POSTPONE +LOOP POSTPONE T[ ; IMMEDIATE
    : REPEAT POSTPONE REPEAT POSTPONE T[ ; IMMEDIATE
    : UNTIL POSTPONE UNTIL POSTPONE T[ ; IMMEDIATE
    : AGAIN POSTPONE AGAIN POSTPONE T[ ; IMMEDIATE --------------------------------------------
    It is insane to mint different words for this behaviour.

    T] and T[ is left as an exercise. Hint: they are state-smart.


    Hans Bezemer

    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat spinning. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxf@21:1/5 to albert on Fri Nov 10 22:35:32 2023
    On 10/11/2023 9:37 pm, albert wrote:
    [..]
    After (in ciforth)
    WANT -scripting-
    you can do *all* control construct in interpreter mode.
    (It is implicit in the -s option, for scripting)

    T] and T[ switch to a temporary compile area. It is typically 4 Gbyte
    away in a 8 Gbyte Forth, but beware of large buffers. (The same
    mechanism is used for classes.)

    Then it is one screen worth of definitions:

    --------------------------------------------
    "T]" WANTED
    : IF T] POSTPONE IF ; IMMEDIATE
    : DO T] POSTPONE DO ; IMMEDIATE
    : ?DO T] POSTPONE ?DO ; IMMEDIATE
    : BEGIN T] POSTPONE BEGIN ; IMMEDIATE
    : THEN POSTPONE THEN POSTPONE T[ ; IMMEDIATE
    : LOOP POSTPONE LOOP POSTPONE T[ ; IMMEDIATE
    : +LOOP POSTPONE +LOOP POSTPONE T[ ; IMMEDIATE
    : REPEAT POSTPONE REPEAT POSTPONE T[ ; IMMEDIATE
    : UNTIL POSTPONE UNTIL POSTPONE T[ ; IMMEDIATE
    : AGAIN POSTPONE AGAIN POSTPONE T[ ; IMMEDIATE --------------------------------------------
    It is insane to mint different words for this behaviour.

    T] and T[ is left as an exercise. Hint: they are state-smart.

    Do you have some examples? I'm still puzzled what these have over the
    regular functions.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to dxforth@gmail.com on Sat Nov 11 11:07:26 2023
    In article <uil4i4$2q6cm$1@dont-email.me>, dxf <dxforth@gmail.com> wrote:
    On 10/11/2023 9:37 pm, albert wrote:
    [..]
    After (in ciforth)
    WANT -scripting-
    you can do *all* control construct in interpreter mode.
    (It is implicit in the -s option, for scripting)

    T] and T[ switch to a temporary compile area. It is typically 4 Gbyte
    away in a 8 Gbyte Forth, but beware of large buffers. (The same
    mechanism is used for classes.)

    Then it is one screen worth of definitions:

    --------------------------------------------
    "T]" WANTED
    : IF T] POSTPONE IF ; IMMEDIATE
    : DO T] POSTPONE DO ; IMMEDIATE
    : ?DO T] POSTPONE ?DO ; IMMEDIATE
    : BEGIN T] POSTPONE BEGIN ; IMMEDIATE
    : THEN POSTPONE THEN POSTPONE T[ ; IMMEDIATE
    : LOOP POSTPONE LOOP POSTPONE T[ ; IMMEDIATE
    : +LOOP POSTPONE +LOOP POSTPONE T[ ; IMMEDIATE
    : REPEAT POSTPONE REPEAT POSTPONE T[ ; IMMEDIATE
    : UNTIL POSTPONE UNTIL POSTPONE T[ ; IMMEDIATE
    : AGAIN POSTPONE AGAIN POSTPONE T[ ; IMMEDIATE
    --------------------------------------------
    It is insane to mint different words for this behaviour.

    T] and T[ is left as an exercise. Hint: they are state-smart.

    Do you have some examples? I'm still puzzled what these have over the >regular functions.



    A typical example is
    ( CRC64 ) CF: ?64 \ AvdH C2feb27

    "BOUNDS" WANTED "-scripting-" WANTED HEX
    \ Well the polynomial
    C96C,5795,D787,0F42 CONSTANT CRC64_POLYNOMIAL \ ECMA-182
    \ Auxiliary table with values for single bytes.
    CREATE CRCTable
    100 0 DO I 8 0 DO
    DUP >R 1 RSHIFT R> 1 AND IF CRC64_POLYNOMIAL XOR THEN
    LOOP , LOOP
    \ For initial CRC and BUFFER COUNT pair, leave the updated CRC
    : CRC-MORE BOUNDS ?DO DUP I C@ XOR 0FF AND CELLS CRCTable + @
    SWAP 8 RSHIFT XOR LOOP ;
    \ For BUFFER COUNT pair, leave the CRC .
    : CRC64 -1 ROT ROT CRC-MORE INVERT ;
    DECIMAL

    ------------------
    Building the crc table you need not worry about a temp definition
    and whether its code land in the middle of the table you want to
    define. Also there is no entry in the dictionary that you never
    want to use again.

    Groetjes Albert
    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat spinning. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxf@21:1/5 to albert on Sun Nov 12 17:36:19 2023
    On 11/11/2023 9:07 pm, albert wrote:
    In article <uil4i4$2q6cm$1@dont-email.me>, dxf <dxforth@gmail.com> wrote:
    On 10/11/2023 9:37 pm, albert wrote:
    [..]
    After (in ciforth)
    WANT -scripting-
    you can do *all* control construct in interpreter mode.
    (It is implicit in the -s option, for scripting)

    T] and T[ switch to a temporary compile area. It is typically 4 Gbyte
    away in a 8 Gbyte Forth, but beware of large buffers. (The same
    mechanism is used for classes.)

    Then it is one screen worth of definitions:

    --------------------------------------------
    "T]" WANTED
    : IF T] POSTPONE IF ; IMMEDIATE
    : DO T] POSTPONE DO ; IMMEDIATE
    : ?DO T] POSTPONE ?DO ; IMMEDIATE
    : BEGIN T] POSTPONE BEGIN ; IMMEDIATE
    : THEN POSTPONE THEN POSTPONE T[ ; IMMEDIATE
    : LOOP POSTPONE LOOP POSTPONE T[ ; IMMEDIATE
    : +LOOP POSTPONE +LOOP POSTPONE T[ ; IMMEDIATE
    : REPEAT POSTPONE REPEAT POSTPONE T[ ; IMMEDIATE
    : UNTIL POSTPONE UNTIL POSTPONE T[ ; IMMEDIATE
    : AGAIN POSTPONE AGAIN POSTPONE T[ ; IMMEDIATE
    --------------------------------------------
    It is insane to mint different words for this behaviour.

    T] and T[ is left as an exercise. Hint: they are state-smart.

    Do you have some examples? I'm still puzzled what these have over the
    regular functions.



    A typical example is
    ( CRC64 ) CF: ?64 \ AvdH C2feb27

    "BOUNDS" WANTED "-scripting-" WANTED HEX
    \ Well the polynomial
    C96C,5795,D787,0F42 CONSTANT CRC64_POLYNOMIAL \ ECMA-182
    \ Auxiliary table with values for single bytes.
    CREATE CRCTable
    100 0 DO I 8 0 DO
    DUP >R 1 RSHIFT R> 1 AND IF CRC64_POLYNOMIAL XOR THEN
    LOOP , LOOP
    \ For initial CRC and BUFFER COUNT pair, leave the updated CRC
    : CRC-MORE BOUNDS ?DO DUP I C@ XOR 0FF AND CELLS CRCTable + @
    SWAP 8 RSHIFT XOR LOOP ;
    \ For BUFFER COUNT pair, leave the CRC .
    : CRC64 -1 ROT ROT CRC-MORE INVERT ;
    DECIMAL

    ------------------
    Building the crc table you need not worry about a temp definition
    and whether its code land in the middle of the table you want to
    define. Also there is no entry in the dictionary that you never
    want to use again.

    I've used FORGET for that but in reality a :NONAME would have done just as well.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to Hans Bezemer on Mon Dec 4 09:33:10 2023
    Hans Bezemer wrote:

    Maybe it is useful to someone. It seems to work just fine. I have no Forth-79 compiler to verify it.

    : ARRAY CELLS ALLOT ;

    I think it's quite obvious but anyways. The curious part is that the author seems to prefer 1-indexed arrays instead of 0-indexed ones.

    Hans Bezemer

    A Portable FORTH Random Number Generator, William T. Doyle
    The Journal of Forth Application and Research Vol. I. Number 2. Dec. 1983

    When seeded with the word RND.INIT the sequences produced are identical
    to those produced by Knuth's FORTRAN subroutine with the same seed and
    number range.

    ANS conversion by Hans Bezemer, 2023
    Converted to a single word version

    I would assume that this is a heuristic RNG with unclear properties,
    even though it was proposed by Knuth and has his nimbus.

    As for fast pseudo RNGs, I have more faith in XOR-shift or Xo(ro)shiro. Although not statistically perfect, Marsaglia's XOR64 is probably the shortest and about the fastest (on 64-bit CPUs) and requires only a single state cell.

    IME it is by far good enough for non-cryptographic needs.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to All on Tue Dec 5 08:40:50 2023
    Marsaglia XOR-shift Pseudo Random Number Generator

    VARIABLE (RND) HERE (RND) ! ( dummy seed must not be zero )

    1 CELLS 4 = [IF] 32-bit Forth
    : RND ( -- r )
    (RND) @
    DUP 13 LSHIFT XOR
    DUP 17 RSHIFT XOR
    DUP 5 LSHIFT XOR
    DUP (RND) ! ;

    [ELSE] 64-bit Forth
    : RND ( -- r )
    (RND) @
    DUP 13 LSHIFT XOR
    DUP 7 RSHIFT XOR
    DUP 17 LSHIFT XOR
    DUP (RND) ! ;

    [THEN]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to All on Tue Dec 5 11:04:21 2023
    VARIABLE (RND) HERE (RND) ! ( dummy seed must not be zero )

    This will 'sporadically" fail on a transputer (signed addresses).

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to mhx on Tue Dec 5 13:48:24 2023
    In article <07301104adbc906a95a6050fa771694e@news.novabbs.com>,
    mhx <mhx@iae.nl> wrote:
    VARIABLE (RND) HERE (RND) ! ( dummy seed must not be zero )

    This will 'sporadically" fail on a transputer (signed addresses).

    Easily fixed :
    VARIABLE (RND) HERE 1+ (RND) ! ( dummy seed must not be zero )

    -marcel
    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat spinning. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to mhx on Tue Dec 5 12:12:20 2023
    mhx wrote:
    VARIABLE (RND) HERE (RND) ! ( dummy seed must not be zero )

    This will 'sporadically" fail on a transputer (signed addresses).

    Your mulled wine must be especially good today! :o)

    For even more mood enhancement: https://www.sciencedirect.com/science/article/pii/S0377042718306265

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to none on Tue Dec 5 14:25:03 2023
    none wrote:
    [..]
    This will 'sporadically" fail on a transputer (signed addresses).

    Easily fixed :
    VARIABLE (RND) HERE 1+ (RND) ! ( dummy seed must not be zero )

    ??

    VARIABLE (RND) HERE 1 OR (RND) !
    or
    CREATE (RND) 1 ,

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to dxf on Tue Dec 5 23:34:25 2023
    dxf wrote:

    On 5/12/2023 10:04 pm, mhx wrote:
    VARIABLE (RND) HERE (RND) ! ( dummy seed must not be zero )

    This will 'sporadically" fail on a transputer (signed addresses).

    Presumably @EXECUTE and all routines that treat address 0 as a special
    case would be problematic too? Perhaps it's a good thing transputers
    are a forgotten technology remembered only by OT forthers :)

    We noticed all that quite soon because some developers had a 16bit
    transputer.

    I rather like(d) the transputer ideas. Unfortunately, they failed to
    deliver on their flagship T9000. On an overhead projector it was
    10 times faster than an 80286.

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)