From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Pip Cet <pipcet@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: master d582356: * src/fns.c (Frandom): Handle bignum `limit`s
Date: Fri, 05 Mar 2021 14:56:29 -0500 [thread overview]
Message-ID: <jwv35x9e63h.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <CAOqdjBfocJseE-Ht-9f-ga+R-5LduFqBqgDbTA5UJPMeS6i0vA@mail.gmail.com> (Pip Cet's message of "Fri, 5 Mar 2021 19:42:09 +0000")
>> -If @var{limit} is a positive fixnum, the value is chosen to be
>> +If @var{limit} is a positive integer, the value is chosen to be
>> nonnegative and less than @var{limit}. Otherwise, the value might be
> Should we add "with every value equally likely" here, or is that
> perfectly obvious?
We could do that, yes. While I do understand what's a probability
distribution, that's about as far as much knowledge goes in this area,
so I'll let others take care of that.
>> +static Lisp_Object
>> +ccall2 (Lisp_Object (f) (ptrdiff_t nargs, Lisp_Object *args),
>> + Lisp_Object arg1, Lisp_Object arg2)
>> +{
>> + Lisp_Object args[2] = {arg1, arg2};
>> + return f (2, args);
>> +}
>
> Can't we use CALLN?
And you thought it'd be funny to wait until after I push the patch to
tell me?
>> + /* Return the remainder, except reject the rare case where
>> + get_random returns a number so close to INTMASK that the
> No longer INTMASK.
Yet close enough ;-)
>> + remainder isn't random. */
>> + Lisp_Object remainder = Frem (val, limit);
>> + if (!NILP (ccall2 (Fleq,
>> + ccall2 (Fminus, val, remainder),
>> + ccall2 (Fminus,
>> + Fash (make_fixnum (1), make_fixnum (bits)),
>> + limit))))
>> + return remainder;
>
> Whenever I see that algorithm, I think it can't possibly be correct,
> but it is :-)
I'll trust you on that.
> That docstring always tricks me into thinking "oh, don't worry about
> passing something invalid, you'll get an error", when in fact, you get
> a fixnum. (random -1)? Random fixnum. (random 1.0)? Random fixnum.
> (random 'many)? Random fixnum.
Yes, this sucks, but I didn't dare to fix it.
I did fix the negative bignum case, tho: it now signals an error ;-)
> But I'm really writing to ask whether it might be a good idea to add
> float support while we're there.
Could be: AFAIK we already have code for it in Calc, so it might be
a small matter of moving the code.
To be honest: I only added support for it because I wanted to write some
randomized tests for 64bit bindat support and my machine is using 32bit
pointers still ;-)
> And, all of this could happen in Lisp, couldn't it? Should it?
You might be right: we should probably export just `get_random` (and the
seeding part) to ELisp and then write the rest in ELisp.
Stefan
next prev parent reply other threads:[~2021-03-05 19:56 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20210305170955.27732.27579@vcs0.savannah.gnu.org>
[not found] ` <20210305170957.AF99920E1B@vcs0.savannah.gnu.org>
2021-03-05 19:42 ` master d582356: * src/fns.c (Frandom): Handle bignum `limit`s Pip Cet
2021-03-05 19:56 ` Stefan Monnier [this message]
2021-03-05 20:13 ` Pip Cet
2021-03-05 20:34 ` Stefan Monnier
2021-03-06 7:42 ` Pip Cet
2021-03-06 8:44 ` Eli Zaretskii
2021-03-06 9:44 ` Pip Cet
2021-03-06 10:56 ` Eli Zaretskii
2021-03-06 13:22 ` Pip Cet
2021-03-06 14:45 ` Eli Zaretskii
2021-03-07 13:27 ` Pip Cet
2021-03-07 14:04 ` Eli Zaretskii
2021-03-07 14:21 ` Pip Cet
2021-03-07 15:22 ` Eli Zaretskii
2021-03-07 17:23 ` Pip Cet
2021-03-07 17:47 ` Eli Zaretskii
2021-03-07 18:37 ` Stefan Monnier
2021-03-07 19:54 ` Andrea Corallo via Emacs development discussions.
2021-03-07 19:55 ` Pip Cet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwv35x9e63h.fsf-monnier+emacs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=emacs-devel@gnu.org \
--cc=pipcet@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).