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 24362 invoked from network); 23 Nov 2022 04:15:17 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 23 Nov 2022 04:15:17 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1669176917; b=DGP7pUNRk3Tsnqxe6oqaR6Xp6NozAMSZHXdZZut2QJvtuV3cdVwfLRdFLvGS6ZZxxtsTSzFGpd CPuWmi5pK5ZxwNWu3iXaMoBA1LllXx8lSU1tnwXVsbstUFXq+dqPH9LVbbXY51l003qCHpw3Sx FqFH7buMvLq0CPaeJx+Xuq6nco8GJQzLVnELvhP6pGWIB67d//7XcqUvLC+RGFvxZ++Sku2ZAX qWkdMbAeUwWs6FnnwT/jjBPCs8Uhtytx2fjDlfDF7/Y60Np1nE1qvoBqXHY8RHH7raGDOj5Duv eqA5jvAmfF6/MbF1hD1PLI96g1583yw3l1A0YI+6J0+C8g==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-oi1-f173.google.com) smtp.remote-ip=209.85.167.173; 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=1669176917; bh=0CqGL4hsZo2PLmDfAP6sS+pG0VtwkNO9mZtQtcAqyDk=; 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=kjUGX6FLSMAlZteR6V0k1CqItQF+D7aWa3ACIOFvDwtFtV9r+FohSNu14pHfz0HRfNTi55r9vX i8K9y6rauvO0b7+h1+/pKeryCa6i/4gVvZeWcLIGAt1lOg3Hm8HBtVKzah98/7djHv0vinD4y5 xtFrMucuUPrBOn60Z92+Tvtq2qt0aAYzcE1+O2X2JwEfbAYPieQ2QhYgPyaljuc4T2krPGPR2D vtAUFg0rVnkkR/sayat+NkFlAvnCsuGsy4LuZpZTD8qsZRqk0k8PI4iJGTVfA4L4Cq8LLPrTzF MuCTtHRLD83ynNZbXbESyI25+I8b4ybtDHNMR2j6E7MvHw==; 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=PdFiyR+Otql5jNbEwby6okVCrVfdNULVpU7B7bawm6E=; b=VuMcbXc2lUwkS6LQv3k2TiX/lj /EtWMBLaicfiI/Cd0sDt5US4gsPjtFQv/QYfHT8caeXqQwm/wbVMzMQm28OGAbiS7rNMzTEb69EQm QEHj4mwjitmOcxqMGDH7KW2UCx+kSBtJ9lYfnzadWhYBK9JmJiKJbYMw+EEHoT1+7gFkEO1SxtLKA hT3SCj6ylweyRb+of53GVrRzKXSqwkpHXJyZN1kPznquLtkPAlqUJXYXW7CXTvmv0nuNAl6A/CO0p SYAHi/w9QFnOmj6qwVHuYnOlitbcs06ODxbzzNzpRhChDdRBl8gkhAmejL18BHz1WCI87z6E4w42r AguqE/oQ==; Received: by zero.zsh.org with local id 1oxhAK-000K5u-Mr; Wed, 23 Nov 2022 04:15:16 +0000 Authentication-Results: zsh.org; iprev=pass (mail-oi1-f173.google.com) smtp.remote-ip=209.85.167.173; 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-f173.google.com ([209.85.167.173]:35445) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1oxhA3-000Jlu-SP; Wed, 23 Nov 2022 04:15:01 +0000 Received: by mail-oi1-f173.google.com with SMTP id h132so17940945oif.2 for ; Tue, 22 Nov 2022 20:14:59 -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=PdFiyR+Otql5jNbEwby6okVCrVfdNULVpU7B7bawm6E=; b=M+mwsXfeZwgyLAVag18F5Ze36HwcGDgvvnMbjWzQpkpl3JybAaBRT2ZoxmEUPjmN2j fe085j80mOyDjenKvNjMbtOL6bmW2unkeq+8qbNK15dZSah+zqTX4DeTXCmIqwV/A1J7 lcx3xVfA2lwD8gyQR5V1xxuY09UBHwnVmQyXPKr5TBj/XIb/PFnkV0ozu6CdWBTFQng4 aw/SJe9u61fqqL3XiZoCSq39jNS9qWbdTkXIOsUtQe2SSCb7if/dshQzdw14NszdQtzf rG4JeaDqmQbUsnabNDzJHSDv2I+UBEAzdMhhLgNPoSPQs3BDd6IL58GB5AQMAaQTQNB8 RrJg== 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=PdFiyR+Otql5jNbEwby6okVCrVfdNULVpU7B7bawm6E=; b=GZq6DO8eYHKYf66FfQkY1plETmyB7OHguR5ogeidcZlw+5U3FJs9Glrz5hHhzd5eFJ ggFxbVaBejPjPYtpZAgVmW/gU41Qt50SujTZl4kF5EQeSRIJE97I/YlYyNSOPv1lBRyW 9DNEnbTh7yYzLpuZ9BFWNkWBq9MOJoyKh6l67bor/Ijj5WDrhyRwiUj8jSfmre6dhv6G Z2nyCxXJvjcZmYbK3Y+NnVdnxMCXt6/p8bnCGYCC8l5H+CIjUtmSyNFPlacO2AbLJ0Zp JqLJp56ONfLTFlNh2C0GJXIWr9dcsYN5LtmAWN6qP4vpSlE7LQz3zTCqoSfVj6bYEHt4 H1gw== X-Gm-Message-State: ANoB5pnxPpTwFpK4vmCWuSNW0kegWKm66cMG+y2BE+R+r6zOlhLjrZmY paOru35LFikbEHKMgqOyY1U= X-Google-Smtp-Source: AA0mqf49KpCNBn2uVORyeubdzqoLmkLd94Dub5zfvLKgI+y/JMKDu95fnPZ1RU0jBqgxq86MYsXyQw== X-Received: by 2002:a05:6808:2211:b0:354:2c52:51eb with SMTP id bd17-20020a056808221100b003542c5251ebmr3798459oib.89.1669176898626; Tue, 22 Nov 2022 20:14:58 -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 h62-20020a9d2f44000000b0066c55e23a16sm6851155otb.2.2022.11.22.20.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 20:14:58 -0800 (PST) Date: Tue, 22 Nov 2022 22:14:55 -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: <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> <2df1001e-69a6-9785-70a6-8416fdcffd8d@zentaur.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2df1001e-69a6-9785-70a6-8416fdcffd8d@zentaur.org> X-Seq: 51029 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 Tue, Nov 22, 2022 at 08:58:30PM -0600, Clinton Bunch wrote: > On 11/22/2022 7:23 PM, Matthew Martin wrote: > > 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. > Instead you get off-by-one errors in new code because people assume the > specified limit is included unless they read the documentation closely. I think exclusive would be more useful. A common use case would be having an array and wanting to choose a random element. If exclusive the code is choices=(red green blue); print ${choices[arc4random_uniform($#choices) + 1]} if inclusive an extra -1 is required. > > > 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. > Even if we go this way,  I don't think the function should be called > arc4random_uniform.  It only implements the arc4random algorithm on *BSD. > > > > 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. > > > > > [snip shell code example] > > That's a lot of zsh code, most of which would be difficult for a non-zsh > expert to write.  So it would need to be provided as a function and then > you're left with the same support problem, but a slower implementation that > keeps converting strings to numbers and back again. It's a lot of code because it's doing a lot. Most users would need just a subset to do their particular task. I'm not suggesting adding the script to zsh; it was just to show SRANDOM and arc4random_uniform are sufficient. > getrandom doesn't need advanced zsh programming knowledge to use.  The -U > and -L make it easy for a relative novice. arc4random_uniform is a > programmer's interface, not a user's interface. I don't agree with this argument. Addition to establish a lower bound is trivial and the subtraction to determine the range is not much harder. Further since getrandom is a builtin not a mathfunc it's more cumbersome to use in the array choice context. > You also ignored the zrandom function which is definitely non-trivial to > implement in the shell.  It's not even trivial to implement in C. It's not clear what a use case for zrandom would be.