standard libraries

Forum for discussion about the documentation project.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

standard libraries

Post by speedfixer »

in KeyPgRandomize
Note:
Randomize is thread-safe (by using an internal mutex), but not thread specific.
Structures for other random number generators are also available in the standard header "fbmath.bi".
When I search for this 'standard' library or header, I can't find it listed anywhere. What else might be in this file?

file.bi
fbgfx.bi
vbcompat.bi
fbc-int/array.bi
< any others >
Perhaps to a list of these 'standard' headers/libraries could be added at "Miscellaneuos" on the first page?

I also would expect to see a reference to them listed in ExtLibTOC or FaqPgrtlib or DevToc.
Things have changed and there are a LOT of goodies that an adventurous person might like to know about. Also, some smart kids WILL figure out how to mix and match this stuff to get features not imagined by us old guys. Some of us may know how to track them down, but that should not be the way to get to these bonus features.

That first page should be the beginning of any search to learn about FreeBASIC. (Well, one has to try the code to really get to "know" any language.)

david
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: standard libraries

Post by fxm »

fbmath.bi is also cited (in addition to RANDOMIZE and RND) in:
- Header Files (.bi)
- Using Prebuilt Libraries
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: standard libraries

Post by speedfixer »

Thanks. I didn't catch those references.

array.bi is missing.

Are the features of fbmath.bi and array.bi described anywhere?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: standard libraries

Post by fxm »

1. array.bi:
2. fbmath.bi:
  • coderJeff wrote: Jul 19, 2021 21:12
    deltarho[1859] wrote:Perhaps coderJeff can give an explanation for fbmath.bi's existence.
    The intent was that 'fbmath.bi' was going to offer some better PRNG's than what was built-in by fbc's randomize & rnd.
    - typically faster even with threads since the generator would not be dependent on any global state - so no need for mutexes
    - allow multiple instances of PRNGs through the TYPE (class) definitions

    But at the time there was like a hundred better offerings in multiple discussion threads on how to seed the PRNG and extending the types with better methods ... I think mostly by you David. :)

    I couldn't keep up with the latest and greatest offers, so fbmath.bi, as-is, is unfinished and needs documentation.

    If you deltarho[1859] (or there is another who is interested maybe with your guidance) want to improve the fbmath.bi header, I would be happy to include the changes in fbc's current version.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: standard libraries

Post by deltarho[1859] »

I'd like to say a few words.

First, I am not a fan of static libraries. All of my PRNG implementations have quite a few private procedures. If an application does not use some, then they don't get compiled.

Second, my PRNG implementations in fbmath.bi are stripped down versions of the original source code and return either an integer random number or a random value in the range [0,1) and no more. In the manual, the fbmath.bi entries are referred to as structures. If more is required then, obviously, additional code is required to work on the integer random number. This already exists in the original source code, which has been stripped down in fbmath.bi. I have then no interest in fbmath.bi snce we cannot, to my mind, better the original source code which has many more procedures.

Anyone coming to FreeBASIC wanting a PRNG will have their work cut out because the implementations are peppered all over the forum. I have also looked at more ways to seed the PRNGs than we can shake a stick at.

All the PRNG implementations pass PractRand to 16TB so from a randomness perspective, it doesn't matter which one we choose. There is a difference speedwise, and it may be worthwhile to benchmark them again with fbc 1.09 and gcc 9.3/-O2. For PRNG work, gas/gas64 does not compete performancewise with optimized gcc in 32-bit or 64-bit modes.

There is no PRNG which shines in both 32-bit and 64-bit mode so, at best, I can only whittle down to two.

Many of the procedures employed are duplicated in the various implementations. Obviously the 64-bit mode PNNG will have some which are not in the 32-bit mode PRNG. What I am thinking of writing is a single bas, called UltimatePRNG.bas, say, with conditional compilation. In 32-bit mode, the best 32-bit mode PRNG engine will be used. In 64-bit mode, the best 64-bit mode PRNG engine will be used and have additional procedures relating to 64-bit.

Fixed seeding will no longer be used. The seeding will be random, and the same technique will be employed for both generators, using what I reckon to be the best seeding method looked at so far. The initial state will be noted and restored, if required, and state snapshots will be available at any time. There is then no need for fixed seeding. We will then be guaranteed good quality seeding, which we cannot do with fixed seeding.

I am now convinced that if a generator requires a warm-up, then we don't need it, no matter how good it is otherwise. During a session, we could enter a section which would need a warm-up if we started there. There is no way that we can determine that we have entered such a section without introducing code which would have a profound impact on the generator's throughput. One guy at PowerBASIC reckoned that no such sections exist. My answer to that is simply: “Why then do some generators require a warm-up?” but not for every entry point. He had no answer to that. I will then be using generators which do not require a warm-up. The worst generator available today requiring a lengthy warm-up is the Mersenne Twister which nobody, it seems, warms up.

So, we can #Include Once “UltimatePRNG.bas”, define 32-bit or 64-bit mode, and we are good to go. Amount of thinking required – none. :)

I do not know when UltimatePRNG.bas will be available, but it will be done, and I will be using it.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: standard libraries

Post by speedfixer »

Thanks for the comments.
I DO already use some of your previously posted code - when needed (rarely.)

I just wanted a user reference of these other official libraries' usage documented somewhere.
Knowing they exist won't help someone use them.

Perhaps I highlighted the wrong text in my first post.
Note:
Randomize is thread-safe (by using an internal mutex), but not thread specific.
Structures for other random number generators are also available in the standard header "fbmath.bi".
What are those other random number generators, how may one use them?

That doc still doesn't exist.

david
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: standard libraries

Post by deltarho[1859] »

speedfixer wrote:What are those other random number generators, how may one use them?

That doc still doesn't exist.
No, it doesn't.

It relies on folk progressing through my posts as they arrived. For anyone just starting with FreeBASIC that is asking too much. That is why I started to think about UltimatePRNG.bas leaving it up to me to select what I reckon to be the best for 32-bit mode and the best for 64-bit mode. I would ask coderJeff to post the thread introducing UltimatePRNG.bas as a sticky.

Meanwhile no one would get the sack for using PCG32II in 32-bit mode or 64-bit mode.

Here is a zipped folder which includes PCG32II.bas and a Help file, PCG32II.chm. That was first published in November 2019.

PCG32II.zip
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: standard libraries

Post by dodicat »

deltarho[]
For a sequence of say 20 numbers:
I notice that Get64Bit can be seeded in Linux the normal way (randomize 100) say.
But is there a way to seed in windows to get the same sequence in windows?
Note I don't actually have Linux just now, but I just had a look at your code.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: standard libraries

Post by deltarho[1859] »

@dodicat

With Linux, FB generator #5 uses /dev/urandom. With Windows, FB generator #5 uses CryptGenRandom which is deprecated. Microsoft says we should now use BCryptGenRandom which was introduced in Windows Vista 15 years ago. PCG32II's Windows seeding uses a CBC-MAC AES-128. I used that in Encrypternet because I don't have Intel's RdSeed on my CPU. RdSeed is multiplicative prediction resistant - CBC-MAC AES-128 should do the same job. Encrypternet is heavy-duty crypto' whereas PCG32II is not, so using CBC-MAC AES-128 is a bit over the top.

Code: Select all

Private Function Get64Bit() As UlongInt ' Will use 'Randomize , 5' by virtue of 'Sub pcg32.MyRandomize'
  Return (Cast( Ulongint, Rnd*(2^32) ) Shl 32) Or Cast( Ulongint, Rnd*(2^32) )
End Function
is fine for Windows seeding as well as Linux.

I am not certain that I have answered your question – have I?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: standard libraries

Post by dodicat »

That's OK deltarho[].
Thanks.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: standard libraries

Post by speedfixer »

You guys don't get it. This topic has NOTHING to do with RANDOMIZE. This is Documentation.

The official docs say there is an official library that supports other random methods.

Any new person looking for that will never find those other methods.
While the forum can help newbies overcome stuck points and guide someone that wants more info about most anything, that 'official' other random library is a myth: no where in official docs.

Searching the forum will indeed let you discover deltarho[1859] posts. I read each and value them all. And all of dodicat's contributions are priceless. But those posts will not tell us anything about fbmath or what was considered 'official' enough to be included in the distribution.

Add to that: a search for 'fbmath.bi' to find the oldest reference to it yields:
or, directly:
There is no 'fbmath.bi' there.

Are you suggesting it is appropriate to expect a person to look through
Search found 248 matches: fbmath.bi
to learn something about it?

Am I alone in seeing this?
Would that inspire you to join if you were looking for a cool language to adopt?

david
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: standard libraries

Post by fxm »

speedfixer wrote: Apr 08, 2022 20:24 The official docs say there is an official library that supports other random methods.
Where is this written?

I only found:
Structures for other random number generators are also available in the standard header "fbmath.bi".
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: standard libraries

Post by speedfixer »

It looks like it was born in this discussion:
Any search hit before this references the project at Sourceforge by Jean Debord

Later in that thread:
by coderJeff » November 14th, 2020, 1:14 pm
Well, I think we are on the right track with this change overall. 'fbmath.bi' needs some work to make it a little more user friendly and some updates to the documentation will help advertise it.
I understand that more work was needed before considered 'complete.'

Has that happened - is it complete?
And if so, where are
some updates to the documentation [that] will help advertise it
???
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: standard libraries

Post by fxm »

In 'fbmath.bi', 5 other PRNGs are defined (each producing either a 'ulong' or a 'double').

Their uses is trivial:
- Declare an instance of the chosen PRNG type.
- Call on this instance the one of 2 member functions repeatedly (returning either a 'ulong', or a 'double' in [0,1)).

Example for the fifth PRNG and 'double':

Code: Select all

#include "fbmath.bi"

Dim As FB.Rndxoroshiro128 r128
For I As Integer = 1 To 20
    Print r128.rnd()
Next I

Sleep
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: standard libraries

Post by deltarho[1859] »

I joined the FreeBASIC forum in January 2017. In February, I ported CryptoRND from PowerBASIC. I then learnt of thread pooling and CryptoRNDII was born. I then did some work with Mersenne Twister. CMWC4096 was ported from PowerBASIC. In June 2017, I got involved with PCG32 before which things had been quiet on the PRNG front.

Sebastiano Vigna knocked out some new generators to replace his ailing earlier versions. Bernard Widynski came up with MsWs and then Squares; in varying flavours. Along comes SFC and then RomuTrio.

I am reminded of the joke about London buses. We wait for nearly an hour without seeing one, and then three turn up. :)

Back in January 2017, I did not foresee the PRNG flurry on the horizon. It happens in science. Someone comes up with a new idea in Quantum Mechanics. Before we know it, half the world's scientists are climbing on the bandwagon.

So, we end up with my PRNG implementations peppered all over the forum.
Yours truly wrote:and return either an integer random number or a random value in the range [0,1) and no more.
fxm wrote:In 'fbmath.bi', 5 other PRNGs are defined (each producing either a 'ulong' or a 'double').
To my mind, that is woefully inadequate, and I have no intention of getting involved with fbmath.bi.

Paul Doe found Bernard Widynski's Middle-Square Weyl Sequence RNG. What we did not know was that it was version 3 coming out in My 2020. Version 1 came out in April 2017 and version 2 came out in July 2019.

Guess what? Version 4 was published a couple of weeks ago, March 22, 2022. The most interesting aspect is the inclusion of a 64-bit output generator. Widynski wrote: “If 32-bit precision is adequate (which is most often the case) msws64 may be the fastest RNG for producing floating-point numbers.” I have always thought that a 53-bit granularity double would not interest many. However, with RomTrio's 64-bit output generator I came up with GetDuo(x,y) which returns two 32-bit granularity doubles from the 64-bit output. GetMono was invented to return one 32-bit granularity double and caches the second. The second one is taken from the cache, and we then generate two more 32-bit granularity doubles and so on.

I am rewriting MsWs to make use of the 64-bit generator. I am scrapping fixed seeding and have introduced an initial state snapshot and a snapshot facility to be implemented at any time thereafter. Fourteen procedures are implemented so far and I will probably make the new MsWs my default generator, forsaking all the other generators that I have looked at. Performance wise it ranks the highest with many procedures and at the top end with the others. So, we will have a good all-rounder for both 32-bit mode and 64-bit mode and replace PCG32II as the best all-rounder.

I really wish that fbmath.bi had never seen the light of day. coderJeff had good intentions, but I disliked the look of it from the start.

The new MsWs will cover most people's needs. We could use it to generate a 32-bit [0,1) and nothing else and have very few procedures compiled. It has a 32-bit output engine and a 64-bit output engine. The source code is at just over 230 lines currently, not much more than PCG32II, but is very intimidating. It will be published with a Help file as with PCG32II.

It is worth noting that once we get to a certain throughput speed a faster throughput will have little bearing on our applications. We could use a faster generator for some procedures, but when we are done, we will probably wonder why we bothered. The new MsWs will do for most of us, if not all. I like Widynski's MsWs maths – he is thorough, and that cannot be said for some of the latest generators. Even PCG32 has been criticized for its lack of rigour, although no one has found it wanting in practice.

There will be no need to search the forum for a PRNG.

Code: Select all

#Include Once "MsWsII.bas"
and we are done.

So, when will MsWsII be available? I don't know – I am pretty thorough as well. :)
Post Reply