From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 22800 invoked from network); 18 Nov 2022 16:26:27 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 18 Nov 2022 16:26:27 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1668788787; b=IMKhb1gVuCrcXCQIC/Y2q46wEmFovf7KoQQ5zXhqyeO7uo9KQC5Xgn3fnPwPUpb+EP9+X3qlOf DaLVW0YsuHSX2cNwOh/1awdqBlqJy48339jhVqht6I5HqM9FQTI3aRLuAhSfI2CWCBhM3FTUNc d+Lpkql+kDaoj88pGUz+7bMPqWgnkXE+hS9n/l9N9lBUSXWmPHW0fTcyi/jjMOC4Koux2QVwR+ Sp3WF41+6YDqICHepGglAEdy6a6TOEaT4Rs92DLCH0QDL3oQ43C4Vb1oCs0Akvw/j9k8+3lPom KDedWRbIzWX8xO+4Nz6mD17c3WUP+4xHG8tS0AHCP1umZA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay9-d.mail.gandi.net) smtp.remote-ip=217.70.183.199; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1668788787; bh=dJvu56pw/Y8uwVhJ+95QLH0lgYTVdPd7uv52EaH5154=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=Ex5l9uRlcnXYq4Ie2wLN0aR072uA7iJDDbPJIThIjo61VXWWb0IzWj1dFQDN6SZWgJhb2K1blt B+vcuxEDeUx4jTK6U56ubbre7I7Q9oC/6JI9NtXoti7rRRixL456uKrjuPivL7J/QBxaHknjQi eVTs/KnFM8oxX1ulo/kmi6uB/v4fRHEPMBebdQ//m7eyXjKVH26l2ns+f1xZPgDIgUTAFIGxVi 4HYuPi4W8CSstLmiS3Ndj7riTpWgjz49WMome6DjyPo+h6uWWJ5isb5hiE56oDpwAqAibWaEbk N8pt7XjWLB4hX/sg2b0wpZDRUiKhNVegYeLf0GeGw6Lkqg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=6tdw/1PJdidVHaQnQ3jjFQIrSKty4a0/tI7fAXLF/aQ=; b=F7nqWHVVE8733Mp64+dhAC9Bul sNslord9YqsNie/W1rUggkX48qtZofxqJdvXTxa3e0bzq6qSlZ22+BzxLATRKCx80HNNmCrsBC1wN ysiCe4S+fUZ6KAS7kchCEuppUddGMGvlYYGyQsWMb8DEdElaDpeNt2srUVOQwiBpiSyQethUg9DPz 85riGkqghgIUDiyn8cobHj5ihGkOih1su15V9alPmiP6xkvYcclPGaPGyPJvqhAv9bXGrBLs/A+q0 Ftj4zMZbRx6x/h0wXXkBoyIDW4IWqfLnwjWxY//1+ufQ1k1bvxbBKX/CYMohXW4PaYVafiwHd8EwC cYNONBCA==; Received: by zero.zsh.org with local id 1ow4C9-000Dhr-GF; Fri, 18 Nov 2022 16:26:25 +0000 Authentication-Results: zsh.org; iprev=pass (relay9-d.mail.gandi.net) smtp.remote-ip=217.70.183.199; dmarc=none header.from=chazelas.org; arc=none Received: from relay9-d.mail.gandi.net ([217.70.183.199]:39493) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1ow49H-000DKB-KV; Fri, 18 Nov 2022 16:23:28 +0000 Received: (Authenticated sender: stephane@chazelas.org) by mail.gandi.net (Postfix) with ESMTPSA id 8A2D0FF80E; Fri, 18 Nov 2022 16:23:25 +0000 (UTC) Date: Fri, 18 Nov 2022 16:23:25 +0000 From: Stephane Chazelas To: Clinton Bunch Cc: zsh-workers@zsh.org Subject: Re: [PATCH] zsh/random module [UPDATED] Message-ID: <20221118162325.7i3qzqljyx4a7z3h@chazelas.org> Mail-Followup-To: Clinton Bunch , zsh-workers@zsh.org References: <741b77be-b679-76cc-f8ec-49c9d89323c1@zentaur.org> <1e8ea669-7a25-b321-6024-72dbc43ac023@zentaur.org> <41205a86-8aad-4821-baa4-1d2ac9bf3c5d@app.fastmail.com> <1b2cafe6-b4b5-c59a-11f3-4dbc1e99e2bc@zentaur.org> <6275a5ac-3a47-f591-7b3c-380ec4fed5ac@zentaur.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6275a5ac-3a47-f591-7b3c-380ec4fed5ac@zentaur.org> X-Seq: 50994 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: I should point out first that I'm not a zsh maintainer. I have been using zsh daily for over 25 years though, and have been following this ML on and off for a few years. I should also point out that I've not read the past discussions about your module. Sorry if I'm repeating points that have been made earlier. Thanks for contributing to zsh, it's always good to get more hands on it! In this instance though I'm not particularly convinced that that new module is particularly needed. We already have $RANDOM and $(( rand48() )). Most if not all of the systems I've ever used had /dev/random / /dev/urandom You can already read n bytes from them with: set +o multibyte read -ru0 -k20 bytes < /dev/random or sysread -s 20 bytes < /dev/urandom Split into individual bytes in an array with: array=( ${(s[])bytes ) convert those from/to decimal/octal/hexadecimal with printf or the # flag or #/## arithmetic operators: hex=(); printf -v hex %02x \'$^array For instance or: set -o extendedglob hex_string=${bytes//(#m)?/${(l[2][0])$(([#16] #MATCH))}} So it seems the functionality of that module could be implemented in a few lines of zsh. Likely less efficiently and more awkwardly, but maybe adding capabilities to decode bytes (a la perl's pack/unpack) would be more useful and could be used for other things than /dev/random. Some comments about the API: > -c COUNT > Sets the number of returned random data. Defaults to 8, > unless -i, -L, or -U is specified without -a, in which case > the default is 1. COUNT must be between 1 and 64. Why those arbitrary limits? Why not from 0 to inf? Without -i you get uint8 in hex and with -i uint32 in decimal. Why the different bases? Why limiting the bounds to unsigned 32 bits when zsh arithmetic is in signed 64 bits? Other than that, it looks good to me. Trying to use it to get a random word with it, I came up with: $ set -o extendedglob $ l=({A..Z} {a..z} {0..9} _) $ getrandom -a a -c20 -L1 -U$#l $ echo ${(j[])a//(#m)*/$l[MATCH]} EMhKvFuQlmCVSuet5uh0 Doing it straight by reading /dev/urandom would have been a lot more awkward. Could that be improved/simplified? -- Stephane