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.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 3559 invoked from network); 23 Nov 2022 01:23:47 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 23 Nov 2022 01:23:47 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1669166627; b=j7Ww4KUH9NVgeZQKn9UVVki+Sb9sMNtb+Ssliw3WQ/numW+528lFg7hs7x/rYhqezJ9A2sgN1s fzkcLfWKUieFRdaOt0X7KZft2DNgRilnFh/MAtai4DMSmD0W0p04U82CnIcRhHQO1NqItJn9Uq P9/otJYdgALkiDUlHY3BPTig6eQ/3KC1gkKvZNTMGkwrBuDBdudMSy0zr8MqQJWg/K7NEZAbyh v5+r3GfkOy1I7XIl2nEVjbHhz+7IPqRYWpCobd9yu1PqDKl4IGgtgrj4XXfx33G/CsxzPvtC7j VmunaZF6SJsi2ulfRgiB/MupN9bJH3xSiXWutTAPdLqo0A==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-oi1-f178.google.com) smtp.remote-ip=209.85.167.178; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1669166627; bh=5zjMlknbX9UwCYD3M3xQa7ZZojNUpG5mu+5u6/csFRQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:DKIM-Signature: DKIM-Signature; b=oilXZA43lvroZgcdng/rmwuwj0bT5/oIaeKOBfUMMrThB1htFBKZVLD8BR9+gCV4iRREgEmz3m hs54Dp1SB5DHVUbPul15mRg2LTwR22VTzXYzB7NvcqFbMa7tncf1XfrRfvyNNLyHVbZHbZhtYm O+0+xOuA4JlKQ+ATaYHV27ziGMTVpbCduGPP2ABLa+vrLEfm+8+KAw3fyawml+R+QuQPGFpTrE ht5+FwCtgDgtOdNzsTCsbmzMX5XISiT2yjZrwXmO42ca+IrzjZ/oQBhgcfuAd9lagDP3YY9ekE GZhIg+yJhXf/nBx1kQ1UlZpHCWBEx8MsUhDHINQ+Ncp4CQ==; 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-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID; bh=xWatD0RaRo4ipC2qS6/DIIjcx3ypgZuc7ZxHpHVeek4=; b=lbgFpLMi4cvkdrA+lV2IhysAz0 7tn4oKcYAxDtr4FFPkw+Hct/JgTguIrg25Yk8HTgyXBvUvee1SoQPTB31NluEuWS3tP9iaMVXIy6R ollHbNTUhC85iz06xp9INRMu517DEx4AmAu9kx+6CO96Xr1OfN1sxV/a53cD5NctP5UVNulMf1DQi r6bs/LI8aS/jmfAdWMjQcrhO2bSgdKdP5e/ql5VC5DvB4Ix+k4tnP/7UaJdnPdxWVbwz4xo7BI0i2 oMTnEULvWifmoxH181FqH0HPdkmjMirAamObq8XuxDPMaWZdQWw4F68Xp/TlVIIyMksYBm0z+b/PI L9d/ICvg==; Received: by zero.zsh.org with local id 1oxeUM-000EQf-4Z; Wed, 23 Nov 2022 01:23:46 +0000 Authentication-Results: zsh.org; iprev=pass (mail-oi1-f178.google.com) smtp.remote-ip=209.85.167.178; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-oi1-f178.google.com ([209.85.167.178]:42596) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1oxeTm-000E3b-G8; Wed, 23 Nov 2022 01:23:11 +0000 Received: by mail-oi1-f178.google.com with SMTP id q186so17650942oia.9 for ; Tue, 22 Nov 2022 17:23:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:from:to:cc:subject:date:message-id:reply-to; bh=xWatD0RaRo4ipC2qS6/DIIjcx3ypgZuc7ZxHpHVeek4=; b=mOSNuztifUKIu4M1oM++4Gq/oDYVrHVYlBHMplVLAW7eHMVIkUfpYEJlOrY1+k983b TOYJeBepo3trOu7GECwJ+SIMKWNJdO+YcNlQY9qbVXdJpEoJMbzFKDjilmozvjiC99ek JIjrvk3MuCbDbGzznXpsfZf6RTCDWe8WgglQaoflo+IJkGPnMEyAnbRQ3jLH//Mpvz0f ax+dv2s0Dwy9Wpe6ENBiiSk7gxbnLwzOqSxmr7HHVjPyHjm6YPk6JAbk3Uf9IJUwt1uv lAs64Ne5R9QE034a7VajqQLXmDTRtO407abIGRAeMCUAxOHKeXsqjllSxchewKw/7FPd UD1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xWatD0RaRo4ipC2qS6/DIIjcx3ypgZuc7ZxHpHVeek4=; b=E0HuYE9YtFzecI6C26q6u83tTSjlhGkjEoc5HflzoctxAsGPupi9E6ICGEYWW0VZBV DUc+ZqolAMxUOUOtpQv0VPXvQbqTYABApzroaGb6BZWEvEP8HF00UVnm+Lcw5h+Nai5p V8UkCFqimDvfdyP4Q5t+Pzch1W2crsf24PIN4eZ5GPgWp7ITazKgRT8mBzpBWmgdgIR3 fdjhRaeDXxlq1nX8FxXBk7wKcTCV3GIoV1Zbnpfo2wnJAgJT+WrMNeM6lvdT7eV3+LN1 WsS7z7Ucemi/q78HHGu+tjYKacjEz336pQySCIIS3IoWQ4ySik/wkSls63Lho61f9fv1 lskQ== X-Gm-Message-State: ANoB5pk04tLCPp5X7mHcoe6z6MuerI++eP9oVxpr9X1YagsJ94FpRGbr /beMzzO8I1AAFWt6R23KLOOBp/z6hDM= X-Google-Smtp-Source: AA0mqf4bEmg1yRQOUyAlHsXvQhAKCshZfESHzxSUYThXWDU7X2FDqQ//s9kv98TgFm8w+o20Vw3UDg== X-Received: by 2002:aca:bc05:0:b0:35a:7141:8e93 with SMTP id m5-20020acabc05000000b0035a71418e93mr15827452oif.124.1669166589116; Tue, 22 Nov 2022 17:23:09 -0800 (PST) Received: from CptOrmolo.darkstar (cpe-76-184-110-212.tx.res.rr.com. [76.184.110.212]) by smtp.gmail.com with ESMTPSA id ca3-20020a056830610300b00661b46cc26bsm6836506otb.9.2022.11.22.17.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 17:23:08 -0800 (PST) Date: Tue, 22 Nov 2022 19:23:05 -0600 From: Matthew Martin To: Clinton Bunch Cc: zsh-workers@zsh.org Subject: Re: [PATCH] zsh/random module [UPDATED] Message-ID: 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> <3423b634-a7c3-9efc-92cd-b9b995ac1c27@zentaur.org> <30a7e749-7f30-ecae-6479-a345b1682e7f@zentaur.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <30a7e749-7f30-ecae-6479-a345b1682e7f@zentaur.org> X-Seq: 51027 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: On Sun, Nov 20, 2022 at 08:57:25PM -0600, Clinton Bunch wrote: > On 11/20/2022 8:21 PM, Matthew Martin wrote: > > If a uniform random function is desired in zsh, I think it should mirror > > the interface of arc4random_uniform: just take an upper_bound and return > > a value in the range [0, upper_bound). > > The original implementation did exclude the upper bound, inadvertently, but > after discussion it seemed that was counter-intuitive and something easily > missed in a cursory glance at the documentation.  Adding a lower bound was > easy enough and saves extra shell code even if it's not likely to be used as > much.  arc4random_uniform excludes the upper bound so it is a direct replacement for % . I think it would be wise to follow an established API and avoid off by one errors when refactoring existing code. Similarly it doesn't offer a lower bound option since that's trivial to implement and difficult to get wrong. > yes, much of getrandom *could* be implemented in shell code, much less > efficiently.  For example my precmd could save the math eval by specifying > getrandom -L 1 -U 7. > > Once you've written the code to access the kernel random source, it seems to > make sense to me to make its output as flexible as possible.  I don't get > the concern about backwards compatibility in implementing a new builtin > unless you know of an external command called getrandom that is in common > use, and that's easily fixed by changing it to zgetrandom. I disagree on making the output as flexible as possible because there are already features in zsh to get those formats and the API would need to be maintained into future releases even if seldom used. I think it would be wise to start with the minimum interface necessary and then build off it as uses arise. Interfacing with libc and the kernel to get random numbers is something that can only be done in C and SRANDOM builds off an existing API, so I have no opposition to it. I can accept that uniformly distributed random integers is a useful and non-trivial task, so probably should be included, but would prefer an existing and proven API like arc4random_uniform. I have a patch based off yours that implements just SRANDOM and a mathfunc for arc4random_uniform, but haven't yet had time to test it on multipl platforms. With just SRANDOM and arc4random_uniform the features of getrandom can be implemented in a script. I've taken a quick shot at a proof of concept below. die() { printf >&2 '%s\n' "$1" exit 1 } zmodload zsh/random local uint32_max=$(( 2**32 - 1 )) local count=8 lower=0 upper=uint32_max result format=hex output=print output_name local -a results while getopts a:c:iL:rs:U: opt; do case $opt in a) output=array; output_name=$OPTARG;; c) count=$OPTARG;; L) lower=$OPTARG;; i) format=int;; r) format=raw; upper=255;; s) output=scalar; output_name=$OPTARG; count=1;; U) upper=$OPTARG;; esac done (( count > 0 )) || die 'count must be at least 1' (( lower >= 0 )) || die 'lower bound must be greater than or equal to 0' if [[ $format == raw ]]; then (( upper <= 255 )) || die 'upper bound must be less than or equal to 255 in raw mode' else (( upper <= uint32_max )) || die "upper bound must be less than or equal to $uint32_max" fi (( lower < upper )) || die 'lower bound must be less than upper bound' if [[ $output == scalar ]] && (( count != 1 )); then die 'count must be 1 for scalar output'; fi repeat count; do if (( lower == 0 && upper == uint32_max )); then result=$SRANDOM else result=$(( arc4random_uniform(upper - lower) + lower )) fi case $format in hex) result=$(( [##16] result ));; int) ;; raw) printf -v result '%b' "\x$(( [##16] result ))" esac results+=($result) done case $output in array) : ${(PA)output_name::=$results};; scalar) : ${(P)output_name::=${results[1]}};; print) print -rn - $results;; esac