From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: master d582356: * src/fns.c (Frandom): Handle bignum `limit`s Date: Fri, 05 Mar 2021 14:56:29 -0500 Message-ID: References: <20210305170955.27732.27579@vcs0.savannah.gnu.org> <20210305170957.AF99920E1B@vcs0.savannah.gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9134"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Pip Cet Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Mar 05 20:57:36 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lIGZr-0002FO-AR for ged-emacs-devel@m.gmane-mx.org; Fri, 05 Mar 2021 20:57:35 +0100 Original-Received: from localhost ([::1]:53944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lIGZq-0001cG-D7 for ged-emacs-devel@m.gmane-mx.org; Fri, 05 Mar 2021 14:57:34 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45476) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIGYv-0001AZ-Kt for emacs-devel@gnu.org; Fri, 05 Mar 2021 14:56:37 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:11577) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIGYr-0008HC-C7 for emacs-devel@gnu.org; Fri, 05 Mar 2021 14:56:36 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 52724440979; Fri, 5 Mar 2021 14:56:31 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 413CE440921; Fri, 5 Mar 2021 14:56:30 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1614974190; bh=9kxTI8QmtewkFi+1vFGk7W0/QJlNJhrcT5K43P37zl8=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=X7iZJQlikNsqnjanhGUY6MVyuvhm4FIMqFnEHpJB/8ymxcYMQJwcRoJA5kUNy7n7m peMQJowlO6R4ebgnN2yInM4vROxCR4dhZGT0HGjArfxx9K2SDAcbQ9t+N5btr4P5ch zU2F1ZHJRD2xsT7zj6BHwlBBcFi5O4gMU9gLcNoFwYYXe6w9QDx0faDx55Y/TbEjmH 5p0HFcJli/hnbyK95iGWHj/aj1ml1gumjUp/62WAPC1ok1I22IgulxoxIXpvRA2re4 qsBC5AEUsFWHpPGrcbyC2hNUdNkVrpOBzImA0FIAZT/ksh5/ZgXAL78f4y6lbRhNk+ U0gafbQ0bTnlQ== Original-Received: from alfajor (unknown [216.154.43.249]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 476D8120264; Fri, 5 Mar 2021 14:56:30 -0500 (EST) In-Reply-To: (Pip Cet's message of "Fri, 5 Mar 2021 19:42:09 +0000") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:266038 Archived-At: >> -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