From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 1a18c4a1 for ; Mon, 2 Dec 2019 15:21:02 +0000 (UTC) Received: (qmail 3383 invoked by alias); 2 Dec 2019 15:20:55 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 44973 Received: (qmail 10894 invoked by uid 1010); 2 Dec 2019 15:20:55 -0000 X-Qmail-Scanner-Diagnostics: from 195-159-176-226.customer.powertech.no by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.0/25649. spamassassin: 3.4.2. Clear:RC:0(195.159.176.226):SA:0(1.6/5.0):. Processed in 1.686623 secs); 02 Dec 2019 15:20:55 -0000 X-Envelope-From: gcszd-zsh-workers@m.gmane.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: none (ns1.primenet.com.au: domain at m.gmane.org does not designate permitted sender hosts) X-Injected-Via-Gmane: http://gmane.org/ To: zsh-workers@zsh.org From: Stephane Chazelas Subject: Re: shuffle array Date: Mon, 2 Dec 2019 15:16:52 +0000 Message-ID: <20191202151652.yke22w7mign4i26q@chaz.gmail.com> References: <867e3jxtao.fsf@zoho.eu> <20191129064215.GA14095@prometheus.u-strasbg.fr> <8636e7w0w9.fsf__36104.0529723809$1575015640$gmane$org@zoho.eu> <20191201200719.anzbs27c7phgdnmm@chaz.gmail.com> <20191201233848.7selcpkvenlu65px__33142.5388505281$1575243619$gmane$org@tarpaulin.shahaf.local2> <20191202141421.sj7nlhsdrpqxpr42@chaz.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: NeoMutt/20180716 Content-Disposition: inline In-Reply-To: 2019-12-02 15:29:17 +0100, Roman Perepelitsa: > Another alternative is to implement the standard inplace shuffle > algorithm from scratch: > > local -i i > for ((i = 2; i <= $#l; ++i)); do > local j=$((RANDOM % i + 1)) shouldn't that be "RANDOM % $#l + 1"? > # swap l[i] and l[j] > local tmp=$l[i] > l[i]=$l[j] > l[j]=$tmp > done > > Due to RANDOM having a rather narrow range, this will introduce bias > on large arrays and won't work at all on arrays with more than 32k > elements. These issues can be mitigated by replacing RANDOM with > (RANDOM << 15 | RANDOM) or even with (RANDOM << 30 | RANDOM << 15 | > RANDOM). [...] Or use rand48() in zsh/mathfunc ((j = 1 + rand48() * $#l)) -- Stephane