From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Omar Polo Newsgroups: gmane.emacs.devel,gmane.emacs.help Subject: Re: urandom number in Emacs Date: Sun, 07 Nov 2021 09:22:10 +0100 Message-ID: <87sfw8s6zt.fsf@omarpolo.com> References: <87lf21nsxe.fsf@zoho.eu> <87bl2woma5.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37975"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.6.9; emacs 29.0.50 Cc: help-gnu-emacs@gnu.org, emacs-devel@gnu.org To: Emanuel Berg Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Nov 07 09:31:35 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 1mjdaQ-0009gc-Kj for ged-emacs-devel@m.gmane-mx.org; Sun, 07 Nov 2021 09:31:34 +0100 Original-Received: from localhost ([::1]:53762 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mjdaP-0000Kx-4a for ged-emacs-devel@m.gmane-mx.org; Sun, 07 Nov 2021 03:31:33 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55872) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mjdZq-00084o-W8; Sun, 07 Nov 2021 03:30:59 -0500 Original-Received: from mail.omarpolo.com ([144.91.116.244]:61202) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mjdZo-00029w-2o; Sun, 07 Nov 2021 03:30:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=omarpolo.com; s=20200327; t=1636273826; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=2Ab7NdNtFiOOb9KA13AZogFfHYY9CeJdNYFA6VFzP/Q=; b=XDqTa79DwlUXKsLj6QYHcFATTUXelmqwbsQCSywi0bWObt7yLJljLLMnTeAKFHJ+/kOO3n Ck9HJ3L52KqFY9ZBnOOoQjxHzYZzhona0VYCh3suyaiABc+l+kDCuJ4UnvOi2Q/wNlCFOd usOtiS0zD3L99J0XcI4NdSa2CludGV4= Original-Received: from localhost (host-79-50-237-209.retail.telecomitalia.it [79.50.237.209]) by mail.omarpolo.com (OpenSMTPD) with ESMTPSA id 3d418968 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 7 Nov 2021 09:30:25 +0100 (CET) Original-Received: from venera (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id 2418fc51; Sun, 7 Nov 2021 09:30:14 +0100 (CET) In-reply-to: <87bl2woma5.fsf@zoho.eu> Received-SPF: pass client-ip=144.91.116.244; envelope-from=op@omarpolo.com; helo=mail.omarpolo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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.29 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:278922 gmane.emacs.help:134436 Archived-At: Emanuel Berg via "Emacs development discussions." writes: >> This is a dynamic module with a function that reads from >> /dev/urandom ... so more random numbers with >> `random-urandom' than with `random' (since Lisp cannot read >> from /dev/urandom because it's a non-regular file - IIUC?) - >> so instead this uses the same C as in pwgen(1) to do it, >> pretty simple I guess :P >> >> So now one can do (random-urandom) ... like any other Lisp >> function and be on everyone else's level. Only cooler B) >> >> See a very long discussion on gmane.emacs.help ... >> >> Anyway, one C file, one C header file, and one Makefile >> here: >> >> https://dataswamp.org/~incal/emacs-init/random-urandom/ >> >> Just do 'make run'. > > ... Hello? > > Information theory guys? Practical guys writing it in Lisp? > Portable and without any external tools? Why, don't run away > all of a sudden? > > Can anyone hear me? > > I mean _this_ time around? I mean, if you really need something like that and emacs doesn't already provide it, why don't try cooking a diff to add it to base emacs instead that an external module? Also, the code is wrong: > if (nbytes == 0) { > return (rnd_num % max_num); > } else { > fprintf(stderr, "No entropy available!\n"); > exit(1); > } I don't think you want to quit emacs unconditionally if there's no entropy. Also, in some circumstances there could be better (and faster) ways to obtain a random number, see for e.g. arc4random. Cheers, Omar Polo P.S.: are you sure about checking for EAGAIN? A read for a file descriptor not marked as non-blocking shouldn't fail with EAGAIN, but that's a minor nitpick.