trealla icon indicating copy to clipboard operation
trealla copied to clipboard

Feature request: How about a bigint random function?

Open Jean-Luc-Picard-2021 opened this issue 2 years ago • 6 comments

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).

Jean-Luc-Picard-2021 avatar Oct 12 '22 17:10 Jean-Luc-Picard-2021

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: @.***>

infradig avatar Oct 12 '22 21:10 infradig

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: @.***>

infradig avatar Oct 12 '22 21:10 infradig

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.

Jean-Luc-Picard-2021 avatar Oct 12 '22 21:10 Jean-Luc-Picard-2021

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: @.***>

infradig avatar Oct 12 '22 22:10 infradig

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.

Jean-Luc-Picard-2021 avatar Oct 12 '22 22:10 Jean-Luc-Picard-2021

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: @.***>

infradig avatar Oct 12 '22 22:10 infradig

Closing, wont fix.

Jean-Luc-Picard-2021 avatar Apr 18 '23 14:04 Jean-Luc-Picard-2021