From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id D1872249F8 for ; Mon, 20 May 2024 19:42:33 +0200 (CEST) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1716226953; b=lkbqO8mWGEan8IsDSquqwpVSFjKXBnROuVaCsK/CFFyrPZzdnMeq2F1GENKAjhFUj23NyD/ULA U2O++2VBvGhgLHfelTB5SIDjj76hceNgDdUUWKXTzK/czmmvg28NfdzUoIHG0EBaj5KOwkgKG4 QYs2mj8zrqP87yYVdArN+ZgzSJ+AyP4CfsXCIMpFjDnHIWCBnJByGGvlFc4uS4NUT/E/1TIU1x yKeg5Yo5b8/CH/om0x1t11fQDRlQPQ4cLzjUOK3Fa3WZF4TwmXsNYDstUbYgYXq68x5AxVrJNm 6d/d1etKiTBPmqe64zco1Hokt3mingKqhC88DHBSApT8VA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-pl1-f172.google.com) smtp.remote-ip=209.85.214.172; dkim=pass header.d=gmail.com header.s=20230601 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=1716226953; bh=XM02EdcwEwFc+dzCgP10G0eMjTX2F07TVQp+DY5JrN0=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:In-Reply-To:From: References:Cc:To:Subject:MIME-Version:Date:Message-ID:DKIM-Signature: DKIM-Signature; b=rnoOTdUHJanXw2GIb9YSeeo1Q2PogpqaoKthr1gD9heBSzWE02gk1X1kwZ9sVBoIvAGSafSywD Eohm4vVs3A0hx8mRJinX07IsHFL2Htw1aqBAb6zTAJx2Slx1eFfcIVSBWGlXRtOYFlLzmrfAiG Ph3BFY3aaMlQLVjpi9j02II/U/JL8Y0v1U2nlIsPGFsYrOiv5n4bkSB/bkJweYWm1brwuICywv 6A5EGzJOorsiWBRqSh9p1p6EdD5lcwstCx0UUfsEK3gIFfBN1c8R1suknewbTwaQxnW1nb8JMr uTnRoca6qvdr6hRBEpVO8llIUs2uMEny/dwqdwomU7wikA==; 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:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=D8uX2J6joNdoDrDs11JDa5buEWiN4CaNC4AqxgBDMYU=; b=F+nmc59zB1Lg5pBzgozjwa1t9P IY9n7rTrIJ76cYGuYXL9o5r6krMwvcAMIZrvU34UuUSdsb2CNKApsfut4tVqxDZh3Momc3ikopmJg CqHI08evkb8mIOJAd+flKFO1XbQTXSoV+wY07EtCamgMx77s+HBCf8KRpRFDnENniVvIf/HlZeQAi ioxnqL+bOgJf6YSHh9G98XbXVbSM/14jIem/6bla1onvynMA9wAQPCWGgcuGYsHNNxCHSpMil5NSF 8JNV/wRrrj07TLy+A5VHuykRInFm4v8I9qfEGJIljbSYe8uUSDC7Etod6QSdZ6+c7Uf8eNAVawJdg TF9GoXgw==; Received: by zero.zsh.org with local id 1s971s-000GmB-DT; Mon, 20 May 2024 17:42:32 +0000 Authentication-Results: zsh.org; iprev=pass (mail-pl1-f172.google.com) smtp.remote-ip=209.85.214.172; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-pl1-f172.google.com ([209.85.214.172]:48502) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1s9714-000G6v-FU; Mon, 20 May 2024 17:41:43 +0000 Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1ee7963db64so14695845ad.1 for ; Mon, 20 May 2024 10:41:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716226901; x=1716831701; darn=zsh.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=D8uX2J6joNdoDrDs11JDa5buEWiN4CaNC4AqxgBDMYU=; b=ULEUsklV7lTyT0M70IALvMtgohCD5fYFyHVvsmBXDsifrSi3Tm33LZm/LD9tvGgjV9 0IKCZyraTBepyJCJj5kzIHlHzI7jC3Za49IdQuvh0sgNa6KhAKQJwBpK75+EVp2BwFMU mcMBn4kwjXzMhfrYc6uRL7cZH9ZGIK1OYNCJZYv7lTM9wTeiwQHVl86ENOqMyXcGk7pP 76aDlCHfrW1ntOAQ752clj0NRm2Fu0Ber0WA2wKt99V4KEdvEB0N+JVS0nGXCflAXQ3Q 1Lul4QRT2DU3yzV/qGZ/CRfc/nZg5oQrE6j1EEW15i76IVR671DJr3NrnBhpe2k/+GlT mTQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716226901; x=1716831701; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D8uX2J6joNdoDrDs11JDa5buEWiN4CaNC4AqxgBDMYU=; b=q8EP3dv+3O5a6CIzZeVeRvSrWb7ZrUNBt42eMZh4TpHfRumW5VFyhSemdayMyIh5uC pzFeYWb+ynyGOULhs0EI6IyPMVFALbscJz0YzjK5T56tUu8CnkG4Iw3UU7moU9+AO7UE ShyXzVGy0JxoF7yJuYgIdH5Jt1KKI+GiXTj3DsXos2CZig3hzM8HuXV3ZjXh21YxboyR vrpY1sXkV8RuHzGTyFvciO+CpXR7MmCmAV/b0PJHIR+aN8lUYNMxJcj7jbVrdx/nUc9R KQZXHq0lLEbNPjJx+UPYGeCEmnrLmJ1AaJeCXtFwrp7CU4ehvHPd5y5GIABKKNhakZaV uIDw== X-Gm-Message-State: AOJu0Yzl/1Nei4T87n9aa8xP0rtEMuWxvER/O7vGYGENlCEPcDFxOILx MFRDVkOF+00m6Oqkh3JhYYt9XwnG5BKMJeyXg3Bst0yskDC51yIK X-Google-Smtp-Source: AGHT+IHhygn3fMzROBsHVQyZyozwXlT99NZbRtqN8TjlOQdta+V1Eyr6jROpLqm4ghVMEFitaSsLKg== X-Received: by 2002:a17:903:988:b0:1f2:f4e6:cdb6 with SMTP id d9443c01a7336-1f2f4e6cf76mr76758235ad.23.1716226900888; Mon, 20 May 2024 10:41:40 -0700 (PDT) Received: from [10.0.0.143] (99-43-252-18.lightspeed.sntcca.sbcglobal.net. [99.43.252.18]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c0361b2sm205048325ad.202.2024.05.20.10.41.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 May 2024 10:41:40 -0700 (PDT) Message-ID: <2fe6d342-8421-4aab-a9f7-062ff85c5546@gmail.com> Date: Mon, 20 May 2024 10:41:39 -0700 MIME-Version: 1.0 User-Agent: Betterbird (Linux) Subject: Re: rigorously predictable random numbers To: Roman Perepelitsa , Ray Andrews Cc: zsh-users@zsh.org References: <2cfbd7e7-0930-44ba-a0bf-99d04762fb92@eastlink.ca> <3a1ecefa-b0f9-4e63-bbf2-bf4dc2822090@app.fastmail.com> <91cc2797-db97-41d4-a06b-207532ec13f4@eastlink.ca> Content-Language: en-US From: Kannan Varadhan In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Seq: 29953 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: Hi folks: This was an interesting pointer.  I am puzzled about some of the zsh-isms here and the logic, so some questions: > while (( $#bytes < 4 )); do > sysread -s$((4-$#bytes)) 'bytes[$#bytes+1]' done It looks like we want 4 individual byes (the sysread promises anywhere from 1 to size of buffer specified, max 8192 (in zshmodules(1)) but may return after only reading 1B.    So why not only say `-s1` instead of the more involved `-s$((4-$#bytes))`? > local b1=$bytes[1] b2=$bytes[2] b3=$bytes[3] b4=$bytes[4] > return '#b1 << 24 | #b2 << 16 | #b3 << 8 | #b4' Now these two lines have me very confused.   Why copy to a scalar parameters and what does referring to them as '#b1', #b2', ... mean.   I have not seen these before and cant figure out where in the zsh documentation this is explained. Thanks Kannan On 5/2/24 21:58, Roman Perepelitsa wrote: > On Fri, May 3, 2024 at 5:48 AM Ray Andrews wrote: >> How does one obtain a genuinely random number then? > There are several ways. I use this: > https://gist.github.com/romkatv/a6cede40714ec77d4da73605c5ddb36a > > # Returns a random 32-bit number. > # If /dev/urandom is cryptographically secure, so is srand32. > # > # If zsh is compiled with 64-bit number support, the result > # is non-negative. Otherwise it may be negative and the value > # is governed by the rules of unsigned-to-signed conversion in C. > # > # Examples: > # > # % print -r -- $(( srand32() )) > # 263510467 > # > # % local -i x='srand32()' > # % typeset -p x > # typeset -i x=858148951 > # > # Performance: ~30us per call. > # > # % time ( repeat 1000000 srand32 ) > # user=19.95s system=6.95s cpu=99% total=26.934 > # > # % uname -srm > # Linux 5.4.0-58-generic x86_64 > # > # % grep 'model name' /proc/cpuinfo | head -1 > # model name : AMD Ryzen 7 3700X 8-Core Processor > function srand32() { > emulate -L zsh -o no_multi_byte > local bytes > while (( $#bytes < 4 )); do > sysread -s$((4-$#bytes)) 'bytes[$#bytes+1]' done > local b1=$bytes[1] b2=$bytes[2] b3=$bytes[3] b4=$bytes[4] > return '#b1 << 24 | #b2 << 16 | #b3 << 8 | #b4' > } > functions -M srand32 0 0 > > Roman >