trealla
trealla copied to clipboard
Feature request: How about a bigint random function?
Was only checking a quick GitHub search and this queries here,
didn't found random/1
as known from SWI-Prolog:
$ ./tpl -v
Trealla Prolog (c) Infradig 2020-2022, v2.4.3
$ ./tpl
?- use_module(library(random)).
true.
?- X is random(10).
error(type_error(evaluable,dummy/dummy),random/1).
?- random(10,X).
error(existence_error(procedure,random/2),top_level/0).
What would be the range of a bigint random value be?
On Thu, Oct 13, 2022 at 3:27 AM Jean-Luc-Picard-2021 < @.***> wrote:
Was only checking a quick GitHub search and this queries here, didn't found random/1 as known from SWI-Prolog:
$ ./tpl -v Trealla Prolog (c) Infradig 2020-2022, v2.4.3 $ ./tpl ?- use_module(library(random)). true. ?- X is random(10). error(type_error(evaluable,dummy/dummy),random/1). ?- random(10,X). error(existence_error(procedure,random/2),top_level/0).
— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/59, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEV3SDVKETOZFDEMX33WC3YG5ANCNFSM6AAAAAARDP7NTI . You are receiving this because you are subscribed to this thread.Message ID: @.***>
Maybe random_between/3 as in SWI does the trick?
On Thu, Oct 13, 2022 at 3:27 AM Jean-Luc-Picard-2021 < @.***> wrote:
Was only checking a quick GitHub search and this queries here, didn't found random/1 as known from SWI-Prolog:
$ ./tpl -v Trealla Prolog (c) Infradig 2020-2022, v2.4.3 $ ./tpl ?- use_module(library(random)). true. ?- X is random(10). error(type_error(evaluable,dummy/dummy),random/1). ?- random(10,X). error(existence_error(procedure,random/2),top_level/0).
— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/59, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEV3SDVKETOZFDEMX33WC3YG5ANCNFSM6AAAAAARDP7NTI . You are receiving this because you are subscribed to this thread.Message ID: @.***>
What would be the range of a bigint random value be?
Any bigint. In SWI-Prolog if you call X is random(M)
, and M is a bigint,
it will generate a bigint in the range [0,M) possibly a smallint sometimes.
So if the bigint has n bits, you would need to create at least n random bits.
Then there is also random/3
and random_between/3
, which both can be
bootstrapped from random/1 evaluable function.
So it should do this...
?- between(1,10,_),X is floor(random_float * 123456789012345678901234567890123456789012345678901234567890). X = 340507627306799885254696646080074420627114708703379456 ; X = 934352929329858954216208081794232293265848069331020152832 ; X = 93794644262735864693165708477367275852722421021982549082112 ; X = 118217920726416122196614248866629095153804390916184956993536 ; X = 74228795681092397624772946658734427425438585719566987427840 ; X = 29341717586621289454751296295137949301227847072683302846464 ; X = 113961996147418490437147992663639043375546997455984938450944 ; X = 5165848055068092963146027341949770512478679338295763140608 ; X = 95847601598247905214854023568519346299780205206085709070336 ; X = 70467594823522672650544771016965636311476303493653923364864.
where 123456789012345678901234567890123456789012345678901234567890 is your M?
On Thu, Oct 13, 2022 at 7:49 AM Jean-Luc-Picard-2021 < @.***> wrote:
What would be the range of a bigint random value be?
Any bigint. In SWI-Prolog if you call X is random(M), and M is a bigint, it will generate a bigint in the range [0,M) possibly a smallint sometimes.
So if the bigint has n bits, you would need to create at least n random bits.
— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/59#issuecomment-1276771373, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEVR6MCOLQUY2GM2HUDWC4W57ANCNFSM6AAAAAARDP7NTI . You are receiving this because you commented.Message ID: @.***>
random_float
usually doesn't generate enought bits for a larger bigint range.
If you have a good quality random_float, it would generate like only 52 bit
or somesuch. So you will have a certain fixed pattern in the lower bits if you
use it with a factor that has more than 52 bit, and if you use it that way for random bigint generation. Thats why for example GMP and Java BigInteger have special bigint random number generators for the purpose of generating
random bigints with enough random bits.
Yes that's what I thought. Unlikely to have time to pursue this.
On Thu, Oct 13, 2022 at 8:08 AM Jean-Luc-Picard-2021 < @.***> wrote:
random_float usually doesn't generate enought bits for a larger bigint range. Only like 52 bits. So you will have a certain fixed pattern in the lower bits if you call a larger bigint range.
— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/59#issuecomment-1276784289, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEQSA6SLROGC6D6D7CTWC4ZGLANCNFSM6AAAAAARDP7NTI . You are receiving this because you commented.Message ID: @.***>
Closing, wont fix.