emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: master d582356: * src/fns.c (Frandom): Handle bignum `limit`s


From: Stefan Monnier
Subject: Re: master d582356: * src/fns.c (Frandom): Handle bignum `limit`s
Date: Fri, 05 Mar 2021 14:56:29 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]