First change in 2026!

15 views
Skip to first unread message

The Beez

unread,
Jan 1, 2026, 8:41:25 AMJan 1
to 4tH-compiler
Hi 4tH-ers!

I wanted to do this for a longer time, but something kept me for actually taking any action. Bu now I've done it: CHOOSE.4TH has been a popular randomizer for as long as I want to remember - but it was fueled by an ancient BSD algorithm. At that moment the idea was - "if it's good enough for Turbo C, it's good enough for me."

But it's not the best RNG in the world. Now, it's replacement is neither, but it's much better anyway: MINSTD in its 1990 form.

I tested the entire range up to the MAXRAND of the BSD algorithm, and it was just fine. If the 32-bit tests do their job as well, this will become permanent. Which would be good news for uBasic/4tH, because its RND() function is based on CHOOSE.4TH ;-)

Hans Bezemer

The Beez

unread,
Jan 1, 2026, 9:28:35 AMJan 1
to 4tH-compiler
Unfortunately, this did not work - but since 32-bits is a dying platform anyway, their CHOOSE will simply not change by the wonders of conditional compilation. 64-bit platforms will now default to MINSTD (unless you override it - you can you know).

For the skeptics, here is a (limited) run of the 64-bit results.

Hans Bezemer
rand2026.txt.zip

The Beez

unread,
Jan 3, 2026, 7:59:20 AM (12 days ago) Jan 3
to 4tH-compiler
Well -- I all found it a bit sad for our 32-bit users. So I started the chase for an updated routine. I found a 16-bit PRNG here: https://lemire.me/blog/2019/07/03/a-fast-16-bit-random-number-generator/

Further research learned it was not bad at all! It was designed by Wang Yi, the 16-bit version (WYhash16) is by Daniel Lemire. The hashing algorithm passes SMHasher and the random number generator passes BigCrush and practrand. As of now it is the fastest algorithm in the SMHasher benchmark (faster than t1ha and XXH3). Furthermore, this algorithm is solid, simple, portable and has no dependencies.

In order to make it work with CHOOSE, I had to restrict it to 15-bits - which is exactly the same thing I had to do to the old BSD random routine. Needless to say - it passed all tests. I will do a few more, as I did with the uBasic/4tH (64bit) executable.

Note I also compared the 4tH version to the original C version - and that matched as well (100000th random number). So it all seems well. And I'll be submitting the changes shortly to SVN.

Hans Bezemer

The Beez

unread,
Jan 3, 2026, 11:57:18 AM (12 days ago) Jan 3
to 4tH-compiler
I was doing the final tests when I bumped into a major deficiency for STDMIN-1990. So I checked OEIS and found out the values were WAY off. The original version, however, was right on the spot (A096550). So I concluded something more than just the multiplication factor must have been changed.

Still, the original version came out fine with the tests. Good distribution.

The improvements for the 32-bit version (with WYhash16) were significant:

BSD  PRNG
Random generator test... Wait!

      0      1      2      3      4      5      6      7      8      9
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
 100030  99634 100228 100523  99941  99930 100567  99915  99752  99480
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
     30   -366    228    523    -59    -70    567    -85   -248   -520

WYhash16 PRNG
Random generator test... Wait!

      0      1      2      3      4      5      6      7      8      9
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
  99992  99973 100092  99965  99977  99997 100092  99899 100023  99990
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
     -8    -27     92    -35    -23     -3     92   -101     23    -10

A much more even distribution. That even translates to 64-bit:

STDMIN-1987
Random generator test... Wait!

      0      1      2      3      4      5      6      7      8      9
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
  99829  99293  99799 100021 100489  99633 100521  99710 100568 100137
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
   -171   -707   -201     21    489   -367    521   -290    568    137

WYhash16 PRNG
Random generator test... Wait!

      0      1      2      3      4      5      6      7      8      9
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
  99976  99973 100072  99972 100007  99986 100084  99901 100019 100010
 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
    -24    -27     72    -28      7    -14     84    -99     19     10

But the extended range of MINSTD seems more fitting in a 64-bit environment. If you don't agree with me, simply include wyhash16.4th before including choose.4th. That's all. However, it proves it's a nifty little routine!

That's all. Soon everything will be in SVN.

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages