Random number generation, seeds, and GMP

Is the claim in the documentation to getrand/1 and setrand/1 still correct? It says,

Errors
- existence_error(random_state, _) is raised if the underlying infrastructure cannot fetch the random state. This is currently the case if SWI-Prolog is not compiled with the GMP library.

I do not know that I have compiled SWI-Prolog with the GMP library. Here is what I see:

$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.3.31-4-ge8b22b3ec)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- current_prolog_flag(gmp_version, X).
false.

?- current_prolog_flag(bounded, X).
X = false.

I have compiled from source, using

$ cmake -DUSE_GMP=OFF -G Ninja ..

and so on.

There seem to be more mentions of GMP in the page on Arithmetic types. Are these cases of leftovers? I do have GMP installed on my system, so it is possible that I just compiled and linked against it.

Well, if you use -DUSE_GMP=OFF you will not link against GMP. The current_prolog_flag(gmp_version, X). confirms this. Without GMP though, SWI-Prolog uses the bundled LibBF implementation to provide unbounded arithmetic, rational numbers and a GMP-independent implementation of the Mersenne Twister random number generator.

Apparently random_property/1 is not implemented when GMP is omitted. I’ll check whether that is a simple omission or not.

Bottom line

  • If you want no bignums at all, use -DUSE_GMP=OFF -DUSE_LIBBF=OFF. That is not recommended as non-bignum arithmetic is rather sloppy. Not even sure it still works.
  • Use GMP if its size does not bother you (on e.g., Linux you share it anyway) and the LGPL license is fine for you. That is the default if GMP is present.
  • Let it fallback to LibBF otherwise. LibBF is typically slower, notably for rational number handling.

LibBF is used by the WASM version as it is much smaller. It was also uses on MacOS for a while because the system crashed at startup with GMP. This is resolved, so we switched back to GMP.

edit Pushed 8b1982e8d040d92c241806c470f6b550e5f5df74 to address this.

Thank you for the explanation! If I understand correctly, then, one can not have GMP and still have getrand/1, setrand/1, rational numbers, and so on. The only difference is efficiency and platform support.

Can it then really be that some of the restrictions documented at the moment are in fact not there? For example, setrand/1 says it will raise an error but in fact works.

Also, in the section on integers we read:

If SWI-Prolog is built using the GNU multiple precision arithmetic library (GMP), integer arithmetic is unbounded

but from what you explain (and from my own observations), that also works with LibBF.

Would it be useful to change the docs or is it going to only get more confusing? With a bit of guidance I might be able to make PRs for that, if it adds any value.

Yes. AFAIK, there is currently no functional difference between SWI-Prolog with GMP and with LibBF. The lack of random_property/1 when using LibBF was a bug.

That would be much appreciated! Just ask if you need further clarification.

1 Like