Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: shuffle array
- X-seq: zsh-workers 44974
- From: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- To: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- Subject: Re: shuffle array
- Date: Mon, 2 Dec 2019 16:27:48 +0100
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- In-reply-to: <20191202151652.yke22w7mign4i26q@chaz.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- 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> <CAN=4vMpt+hQw2zxUC1LH+3bPymBX_5qqexFw-vqkN6ye+dG1ZQ__45487.0572009654$1575297047$gmane$org@mail.gmail.com> <20191202151652.yke22w7mign4i26q@chaz.gmail.com>
On Mon, Dec 2, 2019 at 4:21 PM Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> 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.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author