• KISS 64-bit pseudo-random number generator

    From Krishna Myneni@21:1/5 to All on Sun Sep 8 22:09:03 2024
    This has probably been posted in c.l.f. before (by MHX or others) but
    you can find my Forth implementation of G. Marsaglia's KISS 64-bit PRNG
    at the link below. The file contains both 32-bit and 64-bit PRNGs -- the appropriate one will be loaded for the Forth system (e.g. kForth-64/kForth-32/kForth-Win32). Marsaglia gave a test for the 64-bit
    KISS in his original announcement and this test may be run with the word

    KISS64-TEST

    e.g.

    kiss64-test

    Testing 64-bit RAN-KISS ... PASSED.
    ok

    --
    Krishna Myneni

    https://github.com/mynenik/kForth-64/blob/master/forth-src/kiss.4th

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lars Brinkhoff@21:1/5 to Krishna Myneni on Mon Sep 9 06:55:49 2024
    Krishna Myneni wrote:
    This has probably been posted in c.l.f. before (by MHX or others) but
    you can find my Forth implementation of G. Marsaglia's KISS 64-bit
    PRNG at the link below.

    I would like to recommend Marsaglia's newer and better xorshift family
    of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro. The output (with suitable parameters) is very good*,
    yet the implementation is very simple.

    *But not cryptography grade.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to Lars Brinkhoff on Mon Sep 9 07:41:33 2024
    On Mon, 9 Sep 2024 6:55:49 +0000, Lars Brinkhoff wrote:

    [..]
    I would like to recommend Marsaglia's newer and better xorshift family
    of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro. The output (with suitable parameters) is very good*,
    yet the implementation is very simple.

    *But not cryptography grade.

    Being "cryptography grade" is the point when you want to introduce
    something new for a PRNG :--) Anyway, what does that disclaimer actually
    mean? The simplest NCG-PRNG has only 3 or 4 standard words in it.

    FORTH> locate RANDOM
    File: d:\dfwforth/include/miscutil.frt
    1317:
    1320>> : RANDOM seed $107465 * $234567 + ( -- u )
    1321: 9 ROL DUP TO seed ;

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to mhx on Mon Sep 9 08:55:14 2024
    mhx@iae.nl (mhx) writes:
    On Mon, 9 Sep 2024 6:55:49 +0000, Lars Brinkhoff wrote:

    [..]
    I would like to recommend Marsaglia's newer and better xorshift family
    of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro. The output (with suitable parameters) is very good*,
    yet the implementation is very simple.

    *But not cryptography grade.

    Being "cryptography grade" is the point when you want to introduce
    something new for a PRNG :--)

    Having better randomness at the same speed or better speed with
    similar randomness is also relevant outside cryptographic
    applications.

    1320>> : RANDOM seed $107465 * $234567 + ( -- u )
    1321: 9 ROL DUP TO seed ;

    So this is a linear congruential generator enhanded with the 9 ROL.
    LCGs have known weaknesses that are relevant even for
    non-cryptographic applications. Maybe the ROL fixes those; have you
    run it through ransomness testers?

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2024: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to Anton Ertl on Mon Sep 9 10:04:20 2024
    On Mon, 9 Sep 2024 8:55:14 +0000, Anton Ertl wrote:

    1320>> : RANDOM seed $107465 * $234567 + ( -- u )
    1321: 9 ROL DUP TO seed ;

    So this is a linear congruential generator enhanded with the 9 ROL.
    LCGs have known weaknesses that are relevant even for
    non-cryptographic applications. Maybe the ROL fixes those; have you
    run it through ransomness testers?

    The ROL fixes the problem that the lower bits are "less random" than
    the higher ones (which was npticeable in my graphics applications). With
    9 ROL the generator passes Marsaglia's DIEHARD tests (I showed these
    in an ancient post). However, I see a comment in the source that
    suggests it did not pass a better test than DIEHARD, so that is why
    there are RANF, RAN-NEXT, KISS, pseudo-DES, ran0, ran1, ran2, ran3,
    random3, wurst-rng, isaac, lehmer, mersenne-twister, and some I can't
    find right now.

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to Anton Ertl on Mon Sep 9 12:28:15 2024
    In article <2024Sep9.105514@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    mhx@iae.nl (mhx) writes:
    On Mon, 9 Sep 2024 6:55:49 +0000, Lars Brinkhoff wrote:

    [..]
    I would like to recommend Marsaglia's newer and better xorshift family
    of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro. The output (with suitable parameters) is very good*,
    yet the implementation is very simple.

    *But not cryptography grade.

    Being "cryptography grade" is the point when you want to introduce >>something new for a PRNG :--)

    Having better randomness at the same speed or better speed with
    similar randomness is also relevant outside cryptographic
    applications.

    1320>> : RANDOM seed $107465 * $234567 + ( -- u )
    1321: 9 ROL DUP TO seed ;

    So this is a linear congruential generator enhanded with the 9 ROL.
    LCGs have known weaknesses that are relevant even for
    non-cryptographic applications. Maybe the ROL fixes those; have you
    run it through ransomness testers?

    A naive use of those RANDOM generators without the crutch:
    RANDOM 6 MOD 1+
    (Simulating a roll of the dice)

    Then you discover that rolls of the dice, uneven and odd
    number of eyes alternate ..

    There is much to be said to use a high quality random
    generator to begin with. The expertise to identify faults
    with random generators is high.

    Those types also have a high correlation between subsequent
    points, making it unfit to select a random point in a
    two dimensional plane.

    I know Marcel uses the random generator for games, which is
    okay.

    - anton

    Groetjes Albert
    --
    Temu exploits Christians: (Disclaimer, only 10 apostles)
    Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall
    Art For Home, Office And Garden Decor - Perfect For Windows, Bars,
    And Gifts For Friends Family And Colleagues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to albert@spenarnc.xs4all.nl on Mon Sep 9 15:26:31 2024
    albert@spenarnc.xs4all.nl writes:
    I know Marcel uses the random generator for games, which is
    okay.

    As a player you don't want a bad PRNG in a game.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2024: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Lars Brinkhoff on Mon Sep 9 18:36:40 2024
    On 9/9/24 01:55, Lars Brinkhoff wrote:
    Krishna Myneni wrote:
    This has probably been posted in c.l.f. before (by MHX or others) but
    you can find my Forth implementation of G. Marsaglia's KISS 64-bit
    PRNG at the link below.

    I would like to recommend Marsaglia's newer and better xorshift family
    of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro. The output (with suitable parameters) is very good*,
    yet the implementation is very simple.

    *But not cryptography grade.

    There are a variety of non-crypto grade PRNGs in the kForth
    distributions, which offer different levels of performance as gauged by
    the dieharder tests of Marsaglia. One can generate large files of the
    PRNG sequences and then run dieharder on them -- example code given below.

    At some point it would be worthwhile to document the dieharder metrics
    for these existing PRNGs before adding new ones. I think they provide a
    pretty good range of usable algorithms.

    Various PRNGs are provided in the Forth files below (from the kForth distributions):

    1. These run on 32 or 64-bit Forths and generate either 32-bit or 64-bit numbers (unsigned).

    random.4th -- assorted simple 32/64 bit PRNGs
    kiss.4th -- 32/64 bit KISS PRNGs

    2. This runs on both 32-bit and 64-bit Forths but, at present, generates
    only 32 bit numbers (unsigned) or double precision floating point numbers.

    mersenne.4th -- Mersenne Twister MT19937 (32-bit PRNG)

    3. The following only run on 32-bit Forths at the present time.

    ran-next.4th -- Knuth's PRNG (32-bit only; does not run yet on 64-bit) fsl/isaac.4th -- 32-bit only; does not run yet on 64-bit

    4. The following has been tested on 32-bit Forths, but I don't recall if
    I used/tested the PRNGs under 64-bit yet.

    fsl/extras/noise.4th -- assorted double-prec fp numbers for various
    random distributions (uniform, gaussian, poissonian).

    --
    Krishna


    === begin kiss-test.4th ===
    \ kiss-test.4th
    \
    \ Generate pseudo-random numbers with the KISS PRNG
    \ to use with the dieharder tests.
    \
    \ Two output files are written: kiss1.bin and kiss2.bin.
    \ These files should be concatenated to make one larger
    \ 4GB file, which can then be used with dieharder:
    \
    \ $ cat kiss1.bin kiss2.bin > kiss.bin
    \ $ dieharder -a -g 201 -f kiss.bin
    \
    \ While 4GB of data won't satisfy the requirement for
    \ all of the dieharder tests, it will for most of the
    \ diehard subset of tests. This program can be modified
    \ easily to generate additional 2GB files of data.
    \
    \ K. Myneni, 17 June 2016
    \ http://ccreweb.org

    include strings
    include files
    include kiss

    variable fd
    variable s32

    : next-u ( -- u ) ran-kiss ;

    2variable bytes

    : print-bytes ( -- ) bytes 2@ 1 1000000 m*/ d. ." MB." cr ;

    : output-2GB ( -- )
    536870912 0 DO
    next-u s32 !
    s32 4 fd @ write-file drop
    bytes 2@ 4 s>d d+ bytes 2!
    i 20000000 mod 0 = IF print-bytes THEN
    LOOP
    ;

    : next-file ( c-addr u -- )
    W/O create-file
    ABORT" Unable to create output file!"
    fd !
    output-2GB
    fd @ close-file drop
    ;

    : gen ( -- )
    0 s>d bytes 2!
    s" kiss1.bin" next-file
    s" kiss2.bin" next-file
    ;

    gen
    === end kiss-test.4th ===

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to Anton Ertl on Tue Sep 10 12:00:56 2024
    In article <2024Sep9.172631@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    albert@spenarnc.xs4all.nl writes:
    I know Marcel uses the random generator for games, which is
    okay.

    As a player you don't want a bad PRNG in a game.

    I challenge you to download the tomato game from https://home.hccnet.nl/a.w.m.van.der.horst/games.html
    and run it on a windows emulator.

    Then tell me if a bad, mediocre or good PRNG was used.
    If it is bad, how you plan to exploit a bad PRNG.

    (This game is a first person shooter, inspired by the
    "throwing pie at Bill Gates" game, but more sophisticated,
    e.g. it has more than 20 different targets. )


    - anton

    Groetjes Albert
    --
    Temu exploits Christians: (Disclaimer, only 10 apostles)
    Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall
    Art For Home, Office And Garden Decor - Perfect For Windows, Bars,
    And Gifts For Friends Family And Colleagues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Anton Ertl on Tue Sep 10 19:30:25 2024
    On 9/9/24 03:55, Anton Ertl wrote:
    mhx@iae.nl (mhx) writes:
    On Mon, 9 Sep 2024 6:55:49 +0000, Lars Brinkhoff wrote:

    [..]
    I would like to recommend Marsaglia's newer and better xorshift family
    of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro. The output (with suitable parameters) is very good*,
    yet the implementation is very simple.
    ...
    Having better randomness at the same speed or better speed with
    similar randomness is also relevant outside cryptographic
    applications.


    Supposedly "good" PRNGs give large errors compared to theoretical values
    for some physics simulations. These errors have been studied for a 2D
    Ising model of ferromagnetism at the phase transition temperature, T =
    T_c (transition from ordered spins to disordered spins).

    Ref. [1] shows that a simple 32-bit congruential generator (CONG) gave
    more accurate answers for the average energy <E> and specific heat <C>
    of the model lattice in Monte-Carlo simulations than the supposedly
    superior R250 XOR based shift register generator or a subtract with
    carry generator (SWC) -- incidentally, the R250 generator is included in
    the FSL. All other things being the same for the simulations, the
    following errors (in std deviations) were observed with the different PRNGs:

    PRNG error in <E> error in <C>
    CONG -0.31 0.82
    R250 42.09 -107.16
    SWC -16.95 32.81

    Ref. [2] compares the performance of the following "high quality" PRNGs (Xorshift, Xorwow, Mersenne Twister, and additive lagged Fibonnaci
    generator (ALFG)) on simulation of other theoretical properties of large
    2D Ising models (32768 x 32768). They found the Xorshift PRNG to give
    much larger errors than the other PRNGs. "The other three tested PRNGs, Mersenne Twister, Xorwow, and ALFG, perform well ... staying mostly
    within a few standard errors of their theoretical values."

    Note: I don't know what the difference is between Xorshift and R250 PRNGs.

    --
    Krishna

    References

    1. A. M. Ferrenberg, D. P. Landau, and Y. J. Wang, "Monte Carlo
    Simulations: Hidden Errors from 'Good' Random Number Generators,"
    Physical Review Letters, vol. 69, p. 3382 (1992).

    2. D. Zhu, Y. Lin, G. Sun, and F. Wang, "Critical exponents testing of a
    random number generator with the Wolff cluster algorithm," Journal of Statistical Mechanics: Theory and Experiment, 063202 (2024).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Krishna Myneni on Wed Sep 18 19:28:33 2024
    On 9/12/24 21:10, Krishna Myneni wrote:
    ...

    I ran a simple Monte-Carlo integration of the area in a unit circle
    using the 64-bit LCG PRNG (from random.4th in kForth dist) and
    Marsaglia's 64-bit KISS PRNG. Hard to say which is better for this problem.

    --
    Krishna

    === Comparison of 64-bit LCG and KISS PRNGs ===

    PRNG: RANDOM  (random.4th)
    Ntrials   Area      log(error)
    -------------------------------
    10^2     3.28        -0.86
    10^3     3.18        -1.42
    10^4     3.1724      -1.51
    10^5     3.14048     -2.95
    10^6     3.14350     -2.72
    10^7     3.14225     -3.18
    10^8     3.14152     -4.14

    PRNG: RAN-KISS (kiss.4th)
    Ntrials   Area      log(error)
    -------------------------------
    10^2     3.24       -1.01
    10^3     3.10       -1.38
    10^4     3.1252     -1.79
    10^5     3.14084    -3.12
    10^6     3.14073    -3.06
    10^7     3.14184    -3.61
    10^8     3.14167    -4.11



    A bit more involved test of the same 64-bit PRNGs starts to show the possibility of defects in Marsaglia's 64-bit kiss prng (RAN-KISS)
    compared to a simple 64-bit LCG prng (RANDOM). Instead of drawing from a uniform distribution, as assumed for these generators, the tests below
    draw from a Maxwell-Boltzmann distribution, which describes the
    probability distribution for speeds of ideal gas atoms at a given
    temperature (non-interacting atoms except for hard sphere collisions in
    three dimensions). The different "moments" of the speed may be computed
    from the samples and compared with exact theoretical results: <v>,
    <v^2>, <v^3>, etc., where v is a random sample of the speed for the M-B distribution at given temperature and mass of atom. The parameter "a",
    given by

    a = m/(2*kB*T)

    where m is the mass, T is the temperature, and kB is the Boltzmann
    constant, determines the shape of the M-B distribution. Unsigned 64-bit
    random numbers, generated by the PRNG under test, are converted to
    floating point numbers in the interval [0, 1]. Ideally, the PRNG will distribute these samples uniformly over the interval. The samples are
    converted to random speed samples with an M-B distribution by an inverse mapping of the cumulative probability distribution for M-B to the speed.
    The moments for <v>, <v^2>, and <v^3> are computed from these samples.

    In this simulation, the temperature is set at 300 K, and the mass of the
    He atom is used. Theory predictions for this m and T are

    <v>_th = sqrt( 4/(pi*a) ) = 1258.727 m/s

    <v^2>_th = 3/(2*a) = 1869538 (m/s)^2

    <v^3>_th = sqrt( 16/(pi*a^3) ) = 3140144 x 10^3 (m/s)^3

    Below are the results for the two PRNGs. For N trials of 10^5 through
    10^7, the simple 64-bit LCG prng consistently gives less relative error
    than Marsaglia's 64-bit kiss prng with respect to the theory predictions.

    Source code for these test results is given below.

    --
    Krishna


    === begin tests ===
    Test of prng RANDOM 64-bit (random.4th):

    ' random test-prng

    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1220.2444 1761665.1 2879481740.
    10^3 1258.2315 1844889.5 3025976380.
    10^4 1250.0696 1837249.9 3055346856.
    10^5 1259.9899 1870367.4 3143506292.
    10^6 1259.3923 1868383.4 3136761299.
    10^7 1259.6343 1869366.9 3140010276.

    Relative Errors
    N |e1| |e2| |e3|
    10^2 3.13e-02 5.77e-02 8.30e-02
    10^3 1.19e-03 1.32e-02 3.64e-02
    10^4 7.67e-03 1.73e-02 2.70e-02
    10^5 2.08e-04 4.44e-04 1.07e-03
    10^6 2.66e-04 6.18e-04 1.08e-03
    10^7 7.39e-05 9.15e-05 4.27e-05


    Test of prng RAN-KISS 64-bit (kiss.4th):

    ' ran-kiss test-prng

    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1212.8900 1702106.0 2666594142.
    10^3 1274.7097 1900519.4 3190481667.
    10^4 1259.6892 1866276.3 3123781859.
    10^5 1260.4578 1872500.5 3147907366.
    10^6 1260.2589 1871249.6 3145073404.
    10^7 1259.8296 1869882.9 3140954422.

    Relative Errors
    N |e1| |e2| |e3|
    10^2 3.72e-02 8.96e-02 1.51e-01
    10^3 1.19e-02 1.66e-02 1.60e-02
    10^4 3.03e-05 1.74e-03 5.21e-03
    10^5 5.80e-04 1.58e-03 2.47e-03
    10^6 4.22e-04 9.16e-04 1.57e-03
    10^7 8.11e-05 1.85e-04 2.58e-04

    === end tests ===


    === begin test-prng-mb.4th ===
    \ test-prng-mb.4th
    \
    \ Test a pseudo-random number generator by computing the
    \ moments of powers of speed using random samples from a
    \ Maxwell-Boltzmann distribution of ideal gas speeds in 3D.
    \
    \ Copyright 2024 Krishna Myneni
    \
    \ Permission is granted to use this code for any purpose,
    \ provided the original source is referenced.
    \
    \ Usage Example:
    \
    \ ' random TEST-PRNG
    \
    \ where RANDOM is a user-provided PRNG which returns
    \ a cell-sized pseudo-random number.
    \
    \ Notes
    \
    \ 1. The xt for any PRNG may be passed to the word
    \ TEST-PRNG provided it returns a cell-sized
    \ bit pattern. All bits of the cell are assumed
    \ to be part of the pseudo-random number.
    \
    \ 2. The default M-B distribution is computed for
    \ helium atoms at 300K. If you change the default
    \ temperature or atomic mass, the maximum velocity
    \ used in the root finder call in CDF^-1 may need
    \ to be changed.
    \
    include ans-words
    include strings
    include phyconsts
    include fsl/fsl-util
    include fsl/erf
    include fsl/regfalsi
    include random
    include kiss

    [UNDEFINED] FPICK [IF]
    cr cr .( This program requires a separate FP stack! ) cr
    quit
    [THEN]

    4.0e0 pi f* fconstant 4pi
    -1 0 d>f fconstant F_MAX_U
    300.0e0 fconstant DEF_TEMPERATURE \ K
    1.0e4 fconstant DEF_VMAX \ m/s
    1.0e-9 fconstant DEF_TOL \ root-finder tolerance

    4.002602e0 fconstant m_He \ mass in amu for He atom

    : fcube ( F: r -- r^3 ) fdup fsquare f* ;
    : f3dup 2 fpick 2 fpick 2 fpick ;
    [UNDEFINED] FTUCK [IF]
    : ftuck ( F: a b -- b a b ) fswap fover ;
    [THEN]

    \ Relative error between a value and its reference value
    : rel-error ( F: a aref -- e ) ftuck f- fswap f/ ;

    \ The random number generator to be tested must return a
    \ cell-sized bit pattern (unsigned single) -- all bits of
    \ the cell are assumed to be randomized.
    defer prngU
    defer prngU-init
    ' random is prngU \ default PRNG

    :noname ( -- ) 1000 0 do prngU drop loop ; is prngU-init

    \ Generate a floating point random number between 0 and 1
    \ using the selected unsigned single cell prng.
    : randf ( F: -- r ) prngU 0 d>f F_MAX_U f/ ;

    fvariable m_atom_kg
    : set-atomic-mass ( F: m_amu -- ) kg/amu f* m_atom_kg f! ;

    m_He set-atomic-mass \ default value of mass of atom

    fvariable a

    : set-T ( F: Tk -- )
    kB f* 2.0e0 f* m_atom_kg f@ fswap f/ a f! ;

    \ Return probability density, p(v), for the M-B distribution
    \ with parameter "a"
    : pdf ( F: v -- p )
    fsquare fdup a f@ f* fnegate fexp f*
    4pi f* a f@ pi f/ 1.5e0 f** f* ;

    \ Return cumulative probability, C(v1) i.e. P(0 <= v < v1),
    \ the integral of p(v) from 0 to v1 for the M-B distribution.
    : cdf ( F: v1 -- P )
    fdup fsquare a f@ f* fdup fsqrt erf
    fswap fnegate fexp frot f* 2e f* a f@ pi f/ fsqrt f* f- ;

    \ Invert cdf to obtain velocity from value of cdf
    fvariable pv1
    : cdf^-1 ( F: P -- v )
    pv1 f!
    [: cdf pv1 f@ f- ;] 0.0e0 DEF_VMAX DEF_TOL )root ;

    \ N random trials of velocities following the M-B distribution
    \ are sampled and the moments v, v^2, and v^3 are computed
    \ from the samples and compared to theory.
    fvariable vsum
    fvariable v2sum
    fvariable v3sum
    : mb-prng ( Ntrials -- ) ( F: -- <v> <v^2> <v^3> )
    DEF_TEMPERATURE set-T
    0.0e0 fdup fdup vsum f! v2sum f! v3sum f!
    dup
    0 ?DO
    randf \ random draw of cdf value
    cdf^-1 \ invert cdf to get random v in M-B distribution
    fdup vsum f+!
    fdup fsquare v2sum f+!
    fcube v3sum f+!
    LOOP
    s>f
    vsum f@ fover f/ fswap
    v2sum f@ fover f/ fswap
    v3sum f@ fswap f/ ;

    \ Theoretical moments of v for the M-B distribution at given
    \ temperature and mass, specified by parameter "a".
    : <v>_th ( F: -- <v> ) 4.0e0 pi a f@ f* f/ fsqrt ;
    : <v^2>_th ( F: -- <v^2> ) 3.0e0 2.0e0 a f@ f* f/ ;
    : <v^3>_th ( F: -- <v^3> ) 16.0e0 pi a f@ fcube f* f/ fsqrt ;

    \ Compute relative errors of randomly sampled averages with
    \ respect to the theoretical moments.
    : moment-errors ( F: <v> <v^2> <v^3> -- e1 e2 e3 )
    frot <v>_th rel-error
    frot <v^2>_th rel-error
    frot <v^3>_th rel-error ;


    9 constant MAX-POW

    MAX-POW 1+ 3 float matrix v_mom{{
    MAX-POW 1+ 3 float matrix m_rel{{

    : print-tables ( -- )
    cr ." Moments of speed"
    cr ." N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3" cr
    MAX-POW 1- 2 DO
    v_mom{{ I 2 }} f@ v_mom{{ I 1 }} f@ v_mom{{ I 0 }} f@
    ." 10^" I 1 .r 2 spaces
    12 4 f.rd 2 spaces
    12 1 f.rd 6 spaces
    12 0 f.rd cr
    LOOP
    cr ." Relative Errors"
    cr ." N |e1| |e2| |e3|" cr
    precision \ save precision
    3 set-precision
    MAX-POW 1- 2 DO
    m_rel{{ I 2 }} f@ m_rel{{ I 1 }} f@ m_rel{{ I 0 }} f@
    ." 10^" I 1 .r 2 spaces
    fabs fs. 2 spaces
    fabs fs. 2 spaces
    fabs fs. cr
    LOOP
    set-precision \ restore precision
    cr
    ;

    : test-prng ( xt -- )
    is prngU
    prngU-init
    MAX-POW 1- 2 DO
    I s>f falog fround>s
    mb-prng
    f3dup
    v_mom{{ I 2 }} f! v_mom{{ I 1 }} f! v_mom{{ I 0 }} f!
    moment-errors
    m_rel{{ I 2 }} f! m_rel{{ I 1 }} f! m_rel{{ I 0 }} f!
    LOOP
    print-tables
    ;

    cr cr .( Usage: ' <prng> test-prng )
    cr cr .( e.g. ' random test-prng ) cr cr

    === end test-prng-mb.4th ===

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to Krishna Myneni on Thu Sep 19 06:33:14 2024
    On Thu, 19 Sep 2024 0:28:33 +0000, Krishna Myneni wrote:

    On 9/12/24 21:10, Krishna Myneni wrote:
    [..]
    A bit more involved test of the same 64-bit PRNGs starts to show the possibility of defects in Marsaglia's 64-bit kiss prng (RAN-KISS)
    compared to a simple 64-bit LCG prng (RANDOM).
    [..]

    I wonder if it is at all possible to really prove something
    about the PRNG *with tests of this type*. Intuition wants us to
    believe that the longer we run the simulation, the closer
    the result must match the expected outcome. Shouldn't we
    compute the probability that after a certain size run the
    result does NOT match the known result (given an ideal PRNG),
    or how unlikely it is that the result has a given error?

    Example: say the result of PRNG-a consistently has one of
    its bits (say bit 0) stuck at zero. Would the test under
    consideration detect this specific problem at all?

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to mhx on Thu Sep 19 03:50:14 2024
    On 9/19/24 01:33, mhx wrote:
    On Thu, 19 Sep 2024 0:28:33 +0000, Krishna Myneni wrote:

    On 9/12/24 21:10, Krishna Myneni wrote:
    [..]
    A bit more involved test of the same 64-bit PRNGs starts to show the
    possibility of defects in Marsaglia's 64-bit kiss prng (RAN-KISS)
    compared to a simple 64-bit LCG prng (RANDOM).
    [..]

    I wonder if it is at all possible to really prove something
    about the PRNG *with tests of this type*. Intuition wants us to
    believe that the longer we run the simulation, the closer
    the result must match the expected outcome. Shouldn't we
    compute the probability that after a certain size run the
    result does NOT match the known result (given an ideal PRNG),
    or how unlikely it is that the result has a given error?


    Perfectly valid questions, and also addressable; however, the purpose at present is to *compare the relative performance* of two different prngs
    for a simulation in which the answers are known in the limit of large N. Therefore, while we haven't considered how quickly with N an ideal PRNG
    should converge to the known <v>, <v^2>, <v^3>, a comparison of the
    relative errors of two PRNGs, at given suitably large N, should be a
    valid comparison of the relative performance of the two PRNGs *for this particular problem*.

    Consider that the moments computed from the simulation describe how well
    the histogram of the set of random samples obtained, for fixed N,
    describe the shape of the ideal Maxwell-Boltzmann distribution in the
    limit of large N. The main question which needs to be addressed is
    whether or not N is sufficiently large in the tests shown above for the comparison between PRNG A and PRNG B to be valid.


    Example: say the result of PRNG-a consistently has one of
    its bits (say bit 0) stuck at zero. Would the test under
    consideration detect this specific problem at all?


    This is easy to test, for example with the LCG prng. We can modify the original,

    : random ( -- u ) LCG_MUL seed @ * LCG_ADD + dup seed ! ;

    as follows:

    \ variation of RANDOM with stuck bit 0

    -1 1 LSHIFT constant SB_MASK
    : random-sb ( -- u_sb )
    LCG_MUL seed @ * LCG_ADD + SB_MASK and dup seed ! ;

    Now, run the test on RANDOM-SB

    ' random-sb test-prng

    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1181.0956 1656472.7 2604709063.
    10^3 1293.3130 1952149.7 3300955817.
    10^4 1259.3279 1862988.3 3108515117.
    10^5 1260.5577 1872157.8 3147664636.
    10^6 1259.4425 1868918.9 3139487337.
    10^7 1259.6136 1869145.0 3139092438.

    Relative Errors
    N |e1| |e2| |e3|
    10^2 6.24e-02 1.14e-01 1.71e-01
    10^3 2.67e-02 4.42e-02 5.12e-02
    10^4 3.17e-04 3.50e-03 1.01e-02
    10^5 6.59e-04 1.40e-03 2.39e-03
    10^6 2.26e-04 3.31e-04 2.09e-04
    10^7 9.04e-05 2.10e-04 3.35e-04

    In general, the relative errors shown in this table are higher than for
    the original RANDOM prng: for N=10^7, there is an increase of about a
    factor of 10 in the relative errors for e2 and e3, and a smaller
    increase in error for e1. Only at N = 10^6 are the errors for RANDOM-SB actually smaller than for RANDOM, which suggests that we may need to
    increase N for meaningful comparison.

    --
    Krishna

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to krishna.myneni@ccreweb.org on Thu Sep 19 10:57:00 2024
    In article <vcgok8$gol7$1@dont-email.me>,
    Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
    <SNIP>
    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1181.0956 1656472.7 2604709063.
    10^3 1293.3130 1952149.7 3300955817.
    10^4 1259.3279 1862988.3 3108515117.
    10^5 1260.5577 1872157.8 3147664636.
    10^6 1259.4425 1868918.9 3139487337.
    10^7 1259.6136 1869145.0 3139092438.

    I think for a Monte Carlo simulation at least three tests
    must be done with different seeds.
    <SNIP>
    --
    Krishna



    Groetjes Albert
    --
    Temu exploits Christians: (Disclaimer, only 10 apostles)
    Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall
    Art For Home, Office And Garden Decor - Perfect For Windows, Bars,
    And Gifts For Friends Family And Colleagues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to All on Thu Sep 19 08:18:19 2024
    I would rather rely on Dieharder tests: https://webhome.phy.duke.edu/~rgb/General/dieharder.php

    A perhaps simpler test suite is described here: https://simul.iro.umontreal.ca/testu01/guideshorttestu01.pdf

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Krishna Myneni on Wed Sep 25 18:15:31 2024
    On 9/19/24 06:45, Krishna Myneni wrote:
    On 9/19/24 03:57, albert@spenarnc.xs4all.nl wrote:
    In article <vcgok8$gol7$1@dont-email.me>,
    Krishna Myneni  <krishna.myneni@ccreweb.org> wrote:
    <SNIP>
    Moments of speed
      N       <v> (m/s)    <v^2> (m/s)^2    <v^3> (m/s)^3
    10^2     1181.0956     1656472.7       2604709063.
    10^3     1293.3130     1952149.7       3300955817.
    10^4     1259.3279     1862988.3       3108515117.
    10^5     1260.5577     1872157.8       3147664636.
    10^6     1259.4425     1868918.9       3139487337.
    10^7     1259.6136     1869145.0       3139092438.

    I think for a Monte Carlo simulation at least three tests
    must be done with different seeds.

    Good point. For a meaningful comparison of errors between PRNGs at a
    specific N, the statistical variation of the <v^n> need to be measured
    for different seed values.

    I can add some code to measure this sigma at each N, with 32 seeds
    uniformly spaced between 0 and UMAX.


    I've calculated the statistical variation in the moments for each set of
    N, using 16 different seeds (spaced apart over the interval for UMAX).
    The standard dev. for the 16 <v^i>, computed for N trials is comparable
    to the relative error between the moment and its theoretical value.
    Thus, the relative errors are indeed a meaningful comparison between the
    two prngs tested here, and I think this implies that for N > 10^5 the
    LCG PRNG (RANDOM) gives more accurate answers than the KISS 64 bit PRNG (RAN-KISS), for this problem. The LCG PRNG is faster than the KISS
    64-bit PRNG.

    minforth stated earlier that he would prefer to use diehard tests to
    decide between which of these two PRNGs to use for computing these
    results from random trials. It will be interesting to see if diehard
    tests are consistent with what I find from actually using the PRNGs and comparing the results to the expected results (for large N and ideal PRNG).

    --
    Krishna

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Krishna Myneni on Wed Sep 25 20:45:48 2024
    On 9/25/24 18:15, Krishna Myneni wrote:

    I've calculated the statistical variation in the moments for each set of
    N, using 16 different seeds (spaced apart over the interval for UMAX).
    The standard dev. for the 16 <v^i>, computed for N trials is comparable
    to the relative error between the moment and its theoretical value.
    Thus, the relative errors are indeed a meaningful comparison between the
    two prngs tested here, and I think this implies that for N > 10^5 the
    LCG PRNG  (RANDOM) gives more accurate answers than the KISS 64 bit PRNG (RAN-KISS), for this problem. The LCG PRNG is faster than the KISS
    64-bit PRNG. ...

    The revised test tables for 64-bit prngs RANDOM and RAN-KISS, below,
    include the statistical variations of the moments as a result of
    changing the seed for the prng. These variations are presented as the
    scaled standard deviations for a set of 16 moments, each computed with a different seed. The standard deviations, for a given N,

    {s1, s2, s3} = {sd({<v>(x_i)}), sd({<v^2>(x_i)}), sd({<v^3>(x_i)})}

    where x_i, i=1,16 represents the seed for the random number generator, {<v^j>(x_i)} represents the set of 16 computed moments for each power of
    v, j=1,2,3, and the averaging over the v^j is for N trials.

    These computed standard deviations are scaled by the moments in order to compare these variations with the relative error in the moments,

    s_j/<v^j>

    The relative error, |e_j|, is defined by

    |e_j| = |(<v^j> - <v^j>_th)| / <v^j>_th

    where <v^j>_th is the value of the j^th moment, computed from the
    analytic integral, using the M-B probability density function.

    For example, for the prng RANDOM tests, at N=10^7, the magnitude of the relative error in <v^3>, |e3|, is 4.27e-05 for a random draw of 10^7
    speeds from a Maxwell-Boltzmann distribution. If we repeat this same computation 16 times, each time with a different seed, and look at the normalized standard deviation s3/<v^3> (for N=10^7), this is 3.01e-04.

    Comparing both the relative errors and statistical variations due to
    using different seeds, the tables below show that RANDOM gives
    consistently lower relative errors and statistical seed variations than RAN-KISS, for N >= 10^5.

    --KM


    === RANDOM PRNG TESTS ===
    ' random test-prng

    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1220.2444 1761665.1 2879481740.
    10^3 1258.2315 1844889.5 3025976380.
    10^4 1250.0696 1837249.9 3055346856.
    10^5 1259.9899 1870367.4 3143506292.
    10^6 1259.3923 1868383.4 3136761299.
    10^7 1259.6343 1869366.9 3140010276.

    Relative Errors Seed Variations
    N |e1| |e2| |e3| s1/<v> s2/<v^2> s3/<v^3>
    10^2 3.13e-02 5.77e-02 8.30e-02 4.70e-02 9.67e-02 1.54e-01
    10^3 1.19e-03 1.32e-02 3.64e-02 1.32e-02 2.44e-02 3.59e-02
    10^4 7.67e-03 1.73e-02 2.70e-02 4.43e-03 8.24e-03 1.22e-02
    10^5 2.08e-04 4.44e-04 1.07e-03 1.11e-03 1.95e-03 2.78e-03
    10^6 2.66e-04 6.18e-04 1.08e-03 2.82e-04 6.78e-04 1.27e-03
    10^7 7.39e-05 9.15e-05 4.27e-05 7.76e-05 1.72e-04 3.01e-04

    === RAN-KISS PRNG TESTS ===
    ' ran-kiss test-prng

    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1212.8900 1702106.0 2666594142.
    10^3 1274.7097 1900519.4 3190481667.
    10^4 1259.6892 1866276.3 3123781859.
    10^5 1260.4578 1872500.5 3147907366.
    10^6 1260.2589 1871249.6 3145073404.
    10^7 1259.8296 1869882.9 3140954422.

    Relative Errors Seed Variations
    N |e1| |e2| |e3| s1/<v> s2/<v^2> s3/<v^3>
    10^2 3.72e-02 8.96e-02 1.51e-01 4.07e-02 8.72e-02 1.46e-01
    10^3 1.19e-02 1.66e-02 1.60e-02 1.26e-02 2.54e-02 3.93e-02
    10^4 3.03e-05 1.74e-03 5.21e-03 3.72e-03 6.96e-03 9.85e-03
    10^5 5.80e-04 1.58e-03 2.47e-03 1.26e-03 2.41e-03 3.63e-03
    10^6 4.22e-04 9.16e-04 1.57e-03 4.89e-04 9.75e-04 1.51e-03
    10^7 8.11e-05 1.85e-04 2.58e-04 1.25e-04 2.54e-04 3.99e-04

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to Krishna Myneni on Thu Sep 12 21:10:43 2024
    On 9/10/24 19:30, Krishna Myneni wrote:
    On 9/9/24 03:55, Anton Ertl wrote:
    mhx@iae.nl (mhx) writes:
    On Mon, 9 Sep 2024 6:55:49 +0000, Lars Brinkhoff wrote:

    [..]
    I would like to recommend Marsaglia's newer and better xorshift family >>>> of PRNGs, and preferably the further development by Sebastiano Vigna
    called xoroshiro.  The output (with suitable parameters) is very good*, >>>> yet the implementation is very simple.
    ...
    Having better randomness at the same speed or better speed with
    similar randomness is also relevant outside cryptographic
    applications.


    Supposedly "good" PRNGs give large errors compared to theoretical values
    for some physics simulations. These errors have been studied for a 2D
    Ising model of ferromagnetism at the phase transition temperature, T =
    T_c (transition from ordered spins to disordered spins).

    Ref. [1] shows that a simple 32-bit congruential generator (CONG) gave
    more accurate answers for the average energy <E> and specific heat <C>
    of the model lattice in Monte-Carlo simulations than the supposedly
    superior R250 XOR based shift register generator or a subtract with
    carry generator (SWC) -- incidentally, the R250 generator is included in
    the FSL. All other things being the same for the simulations, the
    following errors (in std deviations) were observed with the different
    PRNGs:

    PRNG   error in <E>   error in <C>
    CONG   -0.31             0.82
    R250   42.09          -107.16
    SWC   -16.95            32.81

    ...

    I ran a simple Monte-Carlo integration of the area in a unit circle
    using the 64-bit LCG PRNG (from random.4th in kForth dist) and
    Marsaglia's 64-bit KISS PRNG. Hard to say which is better for this problem.

    --
    Krishna

    === Comparison of 64-bit LCG and KISS PRNGs ===

    PRNG: RANDOM (random.4th)
    Ntrials Area log(error)
    -------------------------------
    10^2 3.28 -0.86
    10^3 3.18 -1.42
    10^4 3.1724 -1.51
    10^5 3.14048 -2.95
    10^6 3.14350 -2.72
    10^7 3.14225 -3.18
    10^8 3.14152 -4.14

    PRNG: RAN-KISS (kiss.4th)
    Ntrials Area log(error)
    -------------------------------
    10^2 3.24 -1.01
    10^3 3.10 -1.38
    10^4 3.1252 -1.79
    10^5 3.14084 -3.12
    10^6 3.14073 -3.06
    10^7 3.14184 -3.61
    10^8 3.14167 -4.11

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Krishna Myneni on Thu Sep 12 19:15:56 2024
    Krishna Myneni <krishna.myneni@ccreweb.org> writes:
    Marsaglia's 64-bit KISS PRNG. Hard to say which is better for this problem.

    Try either against a cryptographic PRNG and see if there is enough
    difference to satisfy a traditional statistical hypothesis test?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to Paul Rubin on Fri Sep 13 06:07:19 2024
    On Fri, 13 Sep 2024 2:15:56 +0000, Paul Rubin wrote:

    Krishna Myneni <krishna.myneni@ccreweb.org> writes:
    Marsaglia's 64-bit KISS PRNG. Hard to say which is better for this
    problem.

    Try either against a cryptographic PRNG and see if there is enough
    difference to satisfy a traditional statistical hypothesis test?

    I guess these generators need to be initialized. Wouldn't the outcome
    of the integration then depend on the statistical characteristics of
    method used to do that initialization?

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to Paul Rubin on Fri Sep 13 06:56:20 2024
    On Fri, 13 Sep 2024 2:15:56 +0000, Paul Rubin wrote:

    Try either against a cryptographic PRNG

    Do they really exist?? The P stands for Pseudo...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to mhx on Fri Sep 13 03:46:59 2024
    mhx@iae.nl (mhx) writes:
    Try either against a cryptographic PRNG

    I guess these generators need to be initialized. Wouldn't the outcome
    of the integration then depend on the statistical characteristics of
    method used to do that initialization?

    Generally speaking, no, you'd initialize the PRNG with random or
    pseudorandom data. You then get an output stream that is supposed to be indistinguishible from genuine random data.

    minforth@gmx.net (minforth) writes:
    Try either against a cryptographic PRNG
    Do they really exist?? The P stands for Pseudo...

    In crypto jargon, cryptographic PRNG output can't be computationally distinguished from true random data. That is, if you have a
    pseudorandom source and a true random source but you don't know which is
    which, there is no efficient method of distinguishing them that is
    better than guessing.

    If the mathematical theory surrounding this is of interest, the first
    few chapters of these lecture notes are a good place to start. They demystified the topic for me.

    https://web.cs.ucdavis.edu/~rogaway/classes/227/spring05/book/main.pdf

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Krishna Myneni@21:1/5 to albert@spenarnc.xs4all.nl on Thu Sep 19 06:45:15 2024
    On 9/19/24 03:57, albert@spenarnc.xs4all.nl wrote:
    In article <vcgok8$gol7$1@dont-email.me>,
    Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
    <SNIP>
    Moments of speed
    N <v> (m/s) <v^2> (m/s)^2 <v^3> (m/s)^3
    10^2 1181.0956 1656472.7 2604709063.
    10^3 1293.3130 1952149.7 3300955817.
    10^4 1259.3279 1862988.3 3108515117.
    10^5 1260.5577 1872157.8 3147664636.
    10^6 1259.4425 1868918.9 3139487337.
    10^7 1259.6136 1869145.0 3139092438.

    I think for a Monte Carlo simulation at least three tests
    must be done with different seeds.

    Good point. For a meaningful comparison of errors between PRNGs at a
    specific N, the statistical variation of the <v^n> need to be measured
    for different seed values.

    I can add some code to measure this sigma at each N, with 32 seeds
    uniformly spaced between 0 and UMAX.

    --
    KM

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