zsh-workers
 help / color / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Stephane Chazelas <stephane.chazelas@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: shuffle array
Date: Mon, 2 Dec 2019 16:27:48 +0100
Message-ID: <CAN=4vMqN4b2MyCvLXMBWQ_bvaRSY7SSiaGSPRT0bm5z4k3g7Vg@mail.gmail.com> (raw)
In-Reply-To: <20191202151652.yke22w7mign4i26q@chaz.gmail.com>

On Mon, Dec 2, 2019 at 4:21 PM Stephane Chazelas
<stephane.chazelas@gmail.com> wrote:
> shouldn't that be "RANDOM % $#l + 1"?

No, the original code is correct. There is a loop invariant that
l[1,i-1] is shuffled before each iteration. An iteration makes l[1,i]
shuffled.

It's fairly easy to prove that this algorithm will produce each array
permutation with equal probability (assuming that we have access to a
function that gives a random number from [1, N] with uniform
distribution.)

> Or use rand48() in zsh/mathfunc

Thanks, TIL.

Roman.

  reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <867e3jxtao.fsf@zoho.eu>
     [not found] ` <20191129064215.GA14095@prometheus.u-strasbg.fr>
     [not found]   ` <8636e7w0w9.fsf__36104.0529723809$1575015640$gmane$org@zoho.eu>
     [not found]     ` <20191201200719.anzbs27c7phgdnmm@chaz.gmail.com>
     [not found]       ` <20191201233848.7selcpkvenlu65px__33142.5388505281$1575243619$gmane$org@tarpaulin.shahaf.local2>
     [not found]         ` <20191202141421.sj7nlhsdrpqxpr42@chaz.gmail.com>
     [not found]           ` <CAN=4vMpt+hQw2zxUC1LH+3bPymBX_5qqexFw-vqkN6ye+dG1ZQ__45487.0572009654$1575297047$gmane$org@mail.gmail.com>
2019-12-02 15:16             ` Stephane Chazelas
2019-12-02 15:27               ` Roman Perepelitsa [this message]
     [not found] <867e3jxtao.fsf__7557.99096530012$1574997710$gmane$org@zoho.eu>
2019-12-01 19:57 ` Stephane Chazelas

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN=4vMqN4b2MyCvLXMBWQ_bvaRSY7SSiaGSPRT0bm5z4k3g7Vg@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=stephane.chazelas@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git